Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Qt Creator - JNI Error: libjvm.so: cannot open shared object file: No such file or directory

    Installation and Deployment
    2
    4
    1905
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      Ibrahim last edited by

      Hi; I'm trying JNI in Qt Creator in Ubuntu. But I get this error message:
      error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory

      My .pro file:

      QT += core
      QT -= gui
      
      CONFIG += c++11
      CONFIG += -ljvm
      
      TARGET = JNIExample
      CONFIG += console
      CONFIG -= app_bundle
      
      TEMPLATE = app
      
      INCLUDEPATH += /usr/lib/jvm/java-8-oracle/include
      INCLUDEPATH += /usr/lib/jvm/java-8-oracle/include/linux
      LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so
      
      SOURCES += main.cpp
      
      DISTFILES += \
          myclasses/com/xxx/Code.java
      

      My main.cpp file:

      #include <QCoreApplication>
      #include <jni.h>
      #include <iostream>
      using namespace std;
      
      int main(int argc, char* argv[])
      {
        QCoreApplication a(argc, argv);
      
        JavaVM* jvm;
        JNIEnv* env;
        JavaVMInitArgs jvm_args;
        JavaVMOption options[1];
      
        options[0].optionString = "-Djava.class.path=myclasses/com/xxx";
        jvm_args.version = JNI_VERSION_1_8;
        jvm_args.options = options;
        jvm_args.nOptions = 1;
        jvm_args.ignoreUnrecognized = JNI_TRUE;
      
        jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &jvm_args);
        if (res < 0)
        {
          cout << "Cannot create JVM!\n";
          exit(1);
        }
      
        jclass class_ = env->FindClass("Code");
        if (class_ == 0)
        {
          cout << "Code class not found!\n";
          exit(1);
        }
      
        jmethodID method_id = env->GetMethodID(class_, "getMessage", "()V");
        if (method_id == 0)
        {
          cout << "getMessage() method not found!\n";
          exit(1);
        }
      
        env->CallVoidMethod(class_, method_id);
      
        return a.exec();
      }
      

      How can I run JNI code? Thanks.

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Ibrahim last edited by

        @Ibrahim

        LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so

        Wrong usage. It should be something like:

        LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/ -ljvm
        

        And remove CONFIG += -ljvm.
        CONFIG has different purpose.

        The best way to avoid these errors is to add library from QtCreator itself.
        QtCreator > Project's .pro file > Add Library > External Library

        157

        I 1 Reply Last reply Reply Quote 1
        • I
          Ibrahim @p3c0 last edited by Ibrahim

          @p3c0 said in Qt Creator - JNI Error: libjvm.so: cannot open shared object file: No such file or directory:

          @Ibrahim

          LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so

          Wrong usage. It should be something like:

          LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/ -ljvm
          

          And remove CONFIG += -ljvm.
          CONFIG has different purpose.

          The best way to avoid these errors is to add library from QtCreator itself.
          QtCreator > Project's .pro file > Add Library > External Library

          Thanks, it is works! But I get this error: Code class not found!

          Code.java:

          package com.xxx;
          
          public class Code
          {
            public void getMessage()
            {
              System.out.println("Hello from Java!");
            }
          }
          

          Directory:
          Directory

          I tried -Djava.class.path=myclasses/com/xxx and env->FindClass("Code"); like my first message. Also I tried -Djava.class.path=myclasses and env->FindClass("com/xxx/Code");. But both not working. What is reason of Code class not found! message?

          1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators last edited by p3c0

            @Ibrahim Not quite sure but I guess the Java class will need a main method?
            Try adding one.

            157

            1 Reply Last reply Reply Quote 0
            • First post
              Last post