Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Qt Creator - JNI Error: libjvm.so: cannot open shared object file: No such file or directory
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
4 Posts 2 Posters 2.3k Views
  • 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 Offline
    I Offline
    Ibrahim
    wrote on 30 Dec 2016, 19:17 last edited by
    #1

    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.

    P 1 Reply Last reply 31 Dec 2016, 05:14
    0
    • I Ibrahim
      30 Dec 2016, 19:17

      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.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 31 Dec 2016, 05:14 last edited by
      #2

      @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 31 Dec 2016, 13:12
      1
      • P p3c0
        31 Dec 2016, 05:14

        @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

        I Offline
        I Offline
        Ibrahim
        wrote on 31 Dec 2016, 13:12 last edited by Ibrahim
        #3

        @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
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 31 Dec 2016, 14:05 last edited by p3c0
          #4

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

          157

          1 Reply Last reply
          0

          1/4

          30 Dec 2016, 19:17

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved