JNI: Class not found or FindClass returns NULL
-
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:
I tried-Djava.class.path=myclasses/com/xxx
andenv->FindClass("Code");
. Also I tried-Djava.class.path=myclasses
andenv->FindClass("com/xxx/Code");
. But both not working. What is reason ofCode class not found!
message? -
@Ibrahim
There are some open question (at least from my side):- where do you have this example code from?
- how do you execute this code in your android environment?
- how does your Activity and manifest file look like?
-
@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.
-
@Ibrahim
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?