Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How can I run Java Codes in Qt/C++ without QAndroidExtras?
Forum Updated to NodeBB v4.3 + New Features

How can I run Java Codes in Qt/C++ without QAndroidExtras?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 2 Posters 5.3k Views 2 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.
  • I Offline
    I Offline
    Ibrahim
    wrote on 10 Jan 2017, 21:18 last edited by
    #1

    Hi; I'm using Qt 5.7 on Ubuntu 16.04, my Java version is Oracle Java 8. I have got a Java class, it's here:

    package com.pack;
    
    import android.content.Context;
    import android.widget.Toast;
    
    public class AndroidClass
    {
      public static void runToast(Context context)
      {
        Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
      }
    }
    

    I can running this class with QAndroidExtras like this:

    QtAndroid::runOnAndroidThread([]
    {
      QAndroidJniObject::callStaticMethod<void>("com/pack/AndroidClass",
                                                "runToast",
                                                "(Landroid/content/Context;)V",
                                                QtAndroid::androidActivity().object());
    });
    

    But I need to use this Java class with pure JNI (jni.h). I have to choose pure Jni, because I want to use my JNI code both Qt / C++ and C++Builder / FireMonkey. If I choose QAndroidExtras, I can see mismatch and QAndroidExtras license is LGPL license. So, I have to use pure JNI. I wrote a C++ code with pure JNI on Qt, it's here:

    QtAndroid::runOnAndroidThread([]
    {
        JavaVM* jvm;
        JNIEnv* env;
        JavaVMInitArgs jvm_args;
        JavaVMOption options[1];
    
        options[0].optionString = "-Djava.class.path=android/src";
        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)
        {
          qDebug() << "Cannot create JVM!\n";
          exit(1);
        }
    
        jclass class_ = env->FindClass("com/pack/AndroidClass");
        if (class_ == 0)
        {
          qDebug() << "Class not found!\n";
          exit(1);
        }
    
        jmethodID method_id = env->GetStaticMethodID(class_, "runToast", "(Landroid/content/Context;)V");
        if (method_id == 0)
        {
          qDebug() << "runToast() method not found!\n";
          exit(1);
        }
    
        env->CallStaticVoidMethod(class_, method_id, QtAndroid::androidActivity().object());
    });
    

    But when I ran this project on Android, I get the errors:
    alt text
    Whereas I included the necessary commands in the .pro file:

    android: {
        QT += androidextras
        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/ -ljvm
    }
    

    How can I run Java codes in Qt with pure JNI for Qt?
    Also does Qt runs with JavaVM (JNI_CreateJavaVM) on Android? So, I have to create a new JavaVM with pure JNI? Thanks.

    J 1 Reply Last reply 11 Jan 2017, 06:09
    0
    • I Ibrahim
      10 Jan 2017, 21:18

      Hi; I'm using Qt 5.7 on Ubuntu 16.04, my Java version is Oracle Java 8. I have got a Java class, it's here:

      package com.pack;
      
      import android.content.Context;
      import android.widget.Toast;
      
      public class AndroidClass
      {
        public static void runToast(Context context)
        {
          Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
        }
      }
      

      I can running this class with QAndroidExtras like this:

      QtAndroid::runOnAndroidThread([]
      {
        QAndroidJniObject::callStaticMethod<void>("com/pack/AndroidClass",
                                                  "runToast",
                                                  "(Landroid/content/Context;)V",
                                                  QtAndroid::androidActivity().object());
      });
      

      But I need to use this Java class with pure JNI (jni.h). I have to choose pure Jni, because I want to use my JNI code both Qt / C++ and C++Builder / FireMonkey. If I choose QAndroidExtras, I can see mismatch and QAndroidExtras license is LGPL license. So, I have to use pure JNI. I wrote a C++ code with pure JNI on Qt, it's here:

      QtAndroid::runOnAndroidThread([]
      {
          JavaVM* jvm;
          JNIEnv* env;
          JavaVMInitArgs jvm_args;
          JavaVMOption options[1];
      
          options[0].optionString = "-Djava.class.path=android/src";
          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)
          {
            qDebug() << "Cannot create JVM!\n";
            exit(1);
          }
      
          jclass class_ = env->FindClass("com/pack/AndroidClass");
          if (class_ == 0)
          {
            qDebug() << "Class not found!\n";
            exit(1);
          }
      
          jmethodID method_id = env->GetStaticMethodID(class_, "runToast", "(Landroid/content/Context;)V");
          if (method_id == 0)
          {
            qDebug() << "runToast() method not found!\n";
            exit(1);
          }
      
          env->CallStaticVoidMethod(class_, method_id, QtAndroid::androidActivity().object());
      });
      

      But when I ran this project on Android, I get the errors:
      alt text
      Whereas I included the necessary commands in the .pro file:

      android: {
          QT += androidextras
          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/ -ljvm
      }
      

      How can I run Java codes in Qt with pure JNI for Qt?
      Also does Qt runs with JavaVM (JNI_CreateJavaVM) on Android? So, I have to create a new JavaVM with pure JNI? Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 11 Jan 2017, 06:09 last edited by
      #2

      @Ibrahim Are you using 64bit Qt and compiler? I guess the JDK is 32bit and you're trying to use its lib in a 64bit application. Both must be either 32bit or 64bit, but not a mix.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply 11 Jan 2017, 12:21
      1
      • J jsulm
        11 Jan 2017, 06:09

        @Ibrahim Are you using 64bit Qt and compiler? I guess the JDK is 32bit and you're trying to use its lib in a 64bit application. Both must be either 32bit or 64bit, but not a mix.

        I Offline
        I Offline
        Ibrahim
        wrote on 11 Jan 2017, 12:21 last edited by
        #3

        @jsulm thanks, I'm using 64Bit Oracle Java 8. When I compiled code for Desktop 64bit, it is successfuly. But when I compiled code for Android ARMv7 or x86, it gets like the image. (undefined reference to JNI_CreateJavaVM etc.).

        J 1 Reply Last reply 11 Jan 2017, 12:27
        0
        • I Ibrahim
          11 Jan 2017, 12:21

          @jsulm thanks, I'm using 64Bit Oracle Java 8. When I compiled code for Desktop 64bit, it is successfuly. But when I compiled code for Android ARMv7 or x86, it gets like the image. (undefined reference to JNI_CreateJavaVM etc.).

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 11 Jan 2017, 12:27 last edited by
          #4

          @Ibrahim So, you're trying to use a library which is built for x86_64 for an ARM build? This is not going to work.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          I 1 Reply Last reply 11 Jan 2017, 13:16
          1
          • J jsulm
            11 Jan 2017, 12:27

            @Ibrahim So, you're trying to use a library which is built for x86_64 for an ARM build? This is not going to work.

            I Offline
            I Offline
            Ibrahim
            wrote on 11 Jan 2017, 13:16 last edited by
            #5

            @jsulm I found this answer: Link. He said: "you cannot create JavaVM on Android, your app is already running inside a JVM." Well, how can I find running JavaVM for Qt Android app? Also how can I use JavaVM of Qt Android app with pure Jni? I found this: JavaVM *QAndroidJniEnvironment::javaVM(). But I need to add -Djava.class.path=/Java/Sources/Path with options.optionString, because I set my path.
            Thanks.

            J 1 Reply Last reply 11 Jan 2017, 13:18
            0
            • I Ibrahim
              11 Jan 2017, 13:16

              @jsulm I found this answer: Link. He said: "you cannot create JavaVM on Android, your app is already running inside a JVM." Well, how can I find running JavaVM for Qt Android app? Also how can I use JavaVM of Qt Android app with pure Jni? I found this: JavaVM *QAndroidJniEnvironment::javaVM(). But I need to add -Djava.class.path=/Java/Sources/Path with options.optionString, because I set my path.
              Thanks.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 11 Jan 2017, 13:18 last edited by
              #6

              @Ibrahim Qt apps are not running in an Java VM as they are native applications written in C++.
              I don't know whether it is possible to somehow use a Java VM from a Qt app on Android, maybe somebody else knows.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              I 1 Reply Last reply 11 Jan 2017, 15:07
              1
              • J jsulm
                11 Jan 2017, 13:18

                @Ibrahim Qt apps are not running in an Java VM as they are native applications written in C++.
                I don't know whether it is possible to somehow use a Java VM from a Qt app on Android, maybe somebody else knows.

                I Offline
                I Offline
                Ibrahim
                wrote on 11 Jan 2017, 15:07 last edited by Ibrahim 1 Nov 2017, 15:07
                #7

                @jsulm said in How can I run Java Codes in Qt/C++ without QAndroidExtras?:

                Qt apps are not running in an Java VM as they are native applications written in C++.

                I know it, but we can calling / running Android SDK with Java code, I think Java codes are running on JavaVM. Otherwise how do run calling Java codes in Qt with QAndroidExtras?

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Ibrahim
                  wrote on 12 Jan 2017, 17:07 last edited by Ibrahim 1 Dec 2017, 17:08
                  #8

                  I found new jni.h and libjvm.so positions:
                  jni.h positions:

                  /usr/lib/jvm/java-8-oracle/include/jni.h
                  
                  Android/Sdk/ndk-bundle/platforms/android-*/arch-arm/usr/include/jni.h
                  
                  android-studio/jre/include/jni.h
                  

                  libjvm.so positions:

                  /home/username/android-studio/jre/jre/lib/amd64/server/libjvm.so
                  
                  /usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so
                  

                  Also I updated my .pro file like this:

                  #.pro File
                  
                  # FOR ANDROID:
                  android: {
                      QT += androidextras
                      INCLUDEPATH += /home/username/Android/Sdk/ndk-bundle/platforms/android-19/arch-arm/usr/include/
                  }
                  
                  # FOR LINUX-G++ (DESKTOP):
                  linux-g++: {
                      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/ -ljvm
                  }
                  

                  But I get this error: JNI_CreateJavaVM was not declared in this scope.
                  Which should I use jni.h and libjvm.so files for Android ARMv7?

                  J 1 Reply Last reply 13 Jan 2017, 06:02
                  0
                  • I Ibrahim
                    12 Jan 2017, 17:07

                    I found new jni.h and libjvm.so positions:
                    jni.h positions:

                    /usr/lib/jvm/java-8-oracle/include/jni.h
                    
                    Android/Sdk/ndk-bundle/platforms/android-*/arch-arm/usr/include/jni.h
                    
                    android-studio/jre/include/jni.h
                    

                    libjvm.so positions:

                    /home/username/android-studio/jre/jre/lib/amd64/server/libjvm.so
                    
                    /usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so
                    

                    Also I updated my .pro file like this:

                    #.pro File
                    
                    # FOR ANDROID:
                    android: {
                        QT += androidextras
                        INCLUDEPATH += /home/username/Android/Sdk/ndk-bundle/platforms/android-19/arch-arm/usr/include/
                    }
                    
                    # FOR LINUX-G++ (DESKTOP):
                    linux-g++: {
                        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/ -ljvm
                    }
                    

                    But I get this error: JNI_CreateJavaVM was not declared in this scope.
                    Which should I use jni.h and libjvm.so files for Android ARMv7?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 13 Jan 2017, 06:02 last edited by
                    #9

                    @Ibrahim I think /home/username/android-studio/jre is the JRE used by AndroidStudio itself (which is a Java application) - it is NOT for the target device!
                    amd64 means it is for X86_64 and not ARM.
                    You cannot use it on most Android devices.
                    Is there libjvm.so in Android/Sdk/ndk-bundle/platforms/ ...?
                    You really should ask in an Android developer forum or mailing list as your problem is not Qt related.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    I 1 Reply Last reply 13 Jan 2017, 13:22
                    1
                    • J jsulm
                      13 Jan 2017, 06:02

                      @Ibrahim I think /home/username/android-studio/jre is the JRE used by AndroidStudio itself (which is a Java application) - it is NOT for the target device!
                      amd64 means it is for X86_64 and not ARM.
                      You cannot use it on most Android devices.
                      Is there libjvm.so in Android/Sdk/ndk-bundle/platforms/ ...?
                      You really should ask in an Android developer forum or mailing list as your problem is not Qt related.

                      I Offline
                      I Offline
                      Ibrahim
                      wrote on 13 Jan 2017, 13:22 last edited by
                      #10

                      @jsulm thanks. I used this command: locate "libjvm.so" in terminal, and I see 2 libjvm.so positions. There is not libjvm.so in android-19/include.
                      I asked question in Android NDK google+ page. Do you know a platform for this question?

                      1 Reply Last reply
                      0

                      1/10

                      10 Jan 2017, 21:18

                      • Login

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