Qt Creator - JNI Error: libjvm.so: cannot open shared object file: No such file or directory
-
wrote on 30 Dec 2016, 19:17 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.
-
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.
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 -
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 Librarywrote on 31 Dec 2016, 13:12 last edited by Ibrahim@p3c0 said in Qt Creator - JNI Error: libjvm.so: cannot open shared object file: No such file or directory:
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 LibraryThanks, 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!"); } }
I tried
-Djava.class.path=myclasses/com/xxx
andenv->FindClass("Code");
like my first message. Also I tried-Djava.class.path=myclasses
andenv->FindClass("com/xxx/Code");
. But both not working. What is reason ofCode class not found!
message? -
@Ibrahim Not quite sure but I guess the Java class will need a main method?
Try adding one.
1/4