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. JNI: Class not found or FindClass returns NULL
QtWS25 Last Chance

JNI: Class not found or FindClass returns NULL

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.9k Views
  • 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 last edited by
    #1

    Hi; I'm trying JNI with C++. But I get this error: Class not found!.
    Code.java:

    package com.xxx;
    
    public class Code
    {
      public void getMessage()
      {
        System.out.println("Hello World!");
      }
    }
    

    main.cpp:

    #include <jni.h>
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
      JavaVM* jvm;
      JNIEnv* env;
      JavaVMInitArgs jvm_args;
      JavaVMOption options[1];
    
      options[0].optionString = "-Djava.class.path=myclasses";
      jvm_args.version = JNI_VERSION_1_2;
      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("com/xxx/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 0;
    }
    

    Directory for my files:
    Directory
    I tried -Djava.class.path=myclasses/com/xxx and env->FindClass("Code");. Also I tried -Djava.class.path=myclasses and env->FindClass("com/xxx/Code");. But both not working. What is reason of Code class not found! message?

    raven-worxR 1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Ibrahim Have you checked whether that Java code works at first without calling from C++?

      157

      1 Reply Last reply
      0
      • I Ibrahim

        Hi; I'm trying JNI with C++. But I get this error: Class not found!.
        Code.java:

        package com.xxx;
        
        public class Code
        {
          public void getMessage()
          {
            System.out.println("Hello World!");
          }
        }
        

        main.cpp:

        #include <jni.h>
        #include <iostream>
        using namespace std;
        
        int main(int argc, char* argv[])
        {
          JavaVM* jvm;
          JNIEnv* env;
          JavaVMInitArgs jvm_args;
          JavaVMOption options[1];
        
          options[0].optionString = "-Djava.class.path=myclasses";
          jvm_args.version = JNI_VERSION_1_2;
          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("com/xxx/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 0;
        }
        

        Directory for my files:
        Directory
        I tried -Djava.class.path=myclasses/com/xxx and env->FindClass("Code");. Also I tried -Djava.class.path=myclasses and env->FindClass("com/xxx/Code");. But both not working. What is reason of Code class not found! message?

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

        @Ibrahim
        There are some open question (at least from my side):

        1. where do you have this example code from?
        2. how do you execute this code in your android environment?
        3. how does your Activity and manifest file look like?

        --- 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

        I 1 Reply Last reply
        0
        • raven-worxR raven-worx

          @Ibrahim
          There are some open question (at least from my side):

          1. where do you have this example code from?
          2. how do you execute this code in your android environment?
          3. how does your Activity and manifest file look like?
          I Offline
          I Offline
          Ibrahim
          wrote on last edited by
          #4

          @p3c0 yes, Java class is works!
          @raven-worx
          1- I wrote the Java class.
          2 and 3- This application is console app, not Android app.

          But I tried this Java code with QAndroidExtras, and I see **Hello World!" text on terminal. But when I used pure JNI (jni.h file), I saw Class not found!.

          I don't know reason of this message. I researched it, but I did'n find the reason. Why there are not information this problem? I asked very much this question in the place, but I don't get a result.

          raven-worxR 1 Reply Last reply
          0
          • I Ibrahim

            @p3c0 yes, Java class is works!
            @raven-worx
            1- I wrote the Java class.
            2 and 3- This application is console app, not Android app.

            But I tried this Java code with QAndroidExtras, and I see **Hello World!" text on terminal. But when I used pure JNI (jni.h file), I saw Class not found!.

            I don't know reason of this message. I researched it, but I did'n find the reason. Why there are not information this problem? I asked very much this question in the place, but I don't get a result.

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

            @Ibrahim
            have you read the invocation API chapter?

            Please also try to specify an absolute class path.

            --- 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

            I 1 Reply Last reply
            0
            • raven-worxR raven-worx

              @Ibrahim
              have you read the invocation API chapter?

              Please also try to specify an absolute class path.

              I Offline
              I Offline
              Ibrahim
              wrote on last edited by
              #6

              @raven-worx result is same. I didn't set CLASSPATH or LD_LIBRARY_PATH . Is this reason of problem?

              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