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



  • 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.


  • Moderators

    @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



  • @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?


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.