Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. use java api on qt
Forum Updated to NodeBB v4.3 + New Features

use java api on qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.8k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    helenebro
    wrote on last edited by
    #1

    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 ':'
    #endif

    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");
        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

    raven-worxR 1 Reply Last reply
    0
    • H helenebro

      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 ':'
      #endif

      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");
          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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • H Offline
        H Offline
        helenebro
        wrote on last edited by
        #3

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

        raven-worxR 1 Reply Last reply
        0
        • H helenebro

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @helenebro
          and how do you plan to integrate Qt into "plain" Java then?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • H Offline
            H Offline
            helenebro
            wrote on last edited by
            #5

            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

            raven-worxR 1 Reply Last reply
            0
            • H helenebro

              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

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @helenebro
              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.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • H Offline
                H Offline
                helenebro
                wrote on last edited by
                #7

                Ok, Thank you for your help

                1 Reply Last reply
                0

                • Login

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