Important: Please read the Qt Code of Conduct -

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 ';'
    #define PATH_SEPARATOR ':'

    int 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");
        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);
        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);
                }else qDebug()<<"function not found";
            } else qDebug()<<"class not found";
            return 0;
            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.
    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 += \

    I think my file is on the wrong place but i don't know where I have to copy it

  • Moderators

    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 the ANDROID_PACKAGE_SOURCE_DIR variable in your pro file.
    YOu may want to take a looj at the Notification example as reference.

  • Because, I don't want create android application but desktop application.
    If I'm not mistaken, QAndroidJniEnvironment is for android application.

  • Moderators

    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 :

  • Moderators

    What comes in my mind: the (compiled - .class) java file needs to be placed in the classpath, which is options 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.

  • Ok, Thank you for your help

Log in to reply