use java api on qt
-
Hi,
I want call java function from Qt application. I have succeed to create javaVM but I have problem to get my java class.#include <jni.h>
#include <qDebug>
#include <string.h>
#include <windows.h>#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endifint main() { JavaVMOption options[1]; JNIEnv *env; JavaVM *jvm; JavaVMInitArgs vm_args; long status; jclass cls; jmethodID mid; jint square; typedef jint(JNICALL *pCreateJavaVM)(JavaVM **, void**, void *); HINSTANCE hInstance = LoadLibrary(L"C:\\Program Files (x86)\\Java\\jdk1.8.0_101\\jre\\bin\\client\\jvm.dll"); qDebug()<<"histance"<<hInstance; pCreateJavaVM CreateJavaVM = (pCreateJavaVM)GetProcAddress(hInstance, "JNI_CreateJavaVM"); options[0].optionString = "-Djava.class.path=."; vm_args.version = JNI_VERSION_1_2; vm_args.nOptions = 1; vm_args.options = options; vm_args.ignoreUnrecognized = JNI_TRUE; status = CreateJavaVM(&jvm, (void**)&env, &vm_args); qDebug()<<"status"<<status; if (status != JNI_ERR) { //cls = (env)->FindClass("Test"); cls = (env)->FindClass("java/Test"); if(cls !=0) { mid = (env)->GetStaticMethodID( cls, "intMethod", "(I)I"); if(mid !=0) { square = (env)->CallStaticIntMethod(cls, mid, 5); qDebug()<<"square"<<"5²"<<square; }else qDebug()<<"function not found"; } else qDebug()<<"class not found"; (jvm)->DestroyJavaVM(); return 0; } else qDebug()<<"jni error"<<status; return -1; }
public class Test { public static int intMethod(int n) { return n*n; } }
TEMPLATE = app CONFIG += c++11 SOURCES += main.cpp # Default rules for deployment. include(deployment.pri) INCLUDEPATH += "C:/Program Files (x86)/Java/jdk1.8.0_101/include" DEPENDPATH += "C:/Program Files (x86)/Java/jdk1.8.0_101/include" INCLUDEPATH += "C:/Program Files (x86)/Java/jdk1.8.0_101/include/win32" DEPENDPATH += "C:/Program Files (x86)/Java/jdk1.8.0_101/include/win32" LIBS += -L"C:/Program Files (x86)/Java/jdk1.8.0_101/lib/" -ljvm HEADERS += DISTFILES += \ java/Test.java
MyProject/ |__MyProject.pro |__MyProject.pro.user |__deployment.pri |__main.cpp |__java/ |__Test.java
I think my file is on the wrong place but i don't know where I have to copy it
-
@helenebro
Why do you do all the hard work to create a JavaVM object yourself? You can simply use the QAndroidJniEnvironment class.
This also has the advantage that it uses the application class loader which should find your class in the apk.You need to copy the java file into the android folder of your porject and in there in the src folder.
The android folder is specified with theANDROID_PACKAGE_SOURCE_DIR
variable in your pro file.
YOu may want to take a looj at the Notification example as reference. -
@helenebro
and how do you plan to integrate Qt into "plain" Java then? -
It's the opposite. I have a Qt application and I want "call" some java functions. I follow this example : http://www.codeproject.com/Articles/22881/How-to-Call-Java-Functions-from-C-Using-JNI
-
@helenebro
What comes in my mind: the (compiled - .class) java file needs to be placed in the classpath, which isoptions
array variable.Anyway as far as i can see this has nothing to do with Qt.
You should ask this question on a Java forum to get more accurate help.