JNI: Class not found or FindClass returns NULL

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

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


    #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";
      jclass class_ = env->FindClass("com/xxx/Code");
      if (class_ == 0)
        cout << "Code class not found!\n";
      jmethodID method_id = env->GetMethodID(class_, "getMessage", "()V");
      if (method_id == 0)
        cout << "getMessage() method not found!\n";
      env->CallVoidMethod(class_, method_id);
      return 0;

    Directory for my files:
    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?

  • Moderators

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

  • Moderators

    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?

  • @p3c0 yes, Java class is works!
    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.

  • Moderators

    have you read the invocation API chapter?

    Please also try to specify an absolute class path.

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.