I'm trying to call JAVA methods from C++ by JNI. First of all, I got this example, tried it out. I changed the path of JDK and after that I could run the examples and they properly worked.
After that, I tried to import my own JAR file and use one class from it. I copied the code from the example and replaced the classpath and classname with my own:
void MyCPPClass::CallJava()
{
JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
JNIEnv *env; // Pointer to native interface
//==================== prepare loading of Java VM ============================
JavaVMInitArgs vm_args; // Initialization arguments
JavaVMOption* options = new JavaVMOption[ 1 ]; // JVM invocation options
options[ 0 ].optionString = "-Djava.class.path=MyJar.jar"; // where to find java .class
vm_args.version = JNI_VERSION_1_8; // minimum Java version
vm_args.nOptions = 1; // number of options
vm_args.options = options;
vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
//================= load and initialize Java VM and JNI interface ===============
jint rc = JNI_CreateJavaVM( &jvm, (void**)&env, &vm_args ); // YES !!
delete options; // we then no longer need the initialisation options.
//========================= analyse errors if any ==============================
// if process interuped before error is returned, it's because jvm.dll can't be
// found, i.e. its directory is not in the PATH.
if( rc != JNI_OK )
{
exit( EXIT_FAILURE );
}
jclass cls1 = env->FindClass( "aaa/bbb/MyClass" );
...
}
The JAR contains only one class aaa.bbb.MyClass and it's made with IntelliJ IDEA and mvn package command. I copied the JAR file next to my executable.
A value of rc is always 0 (JNI_OK), but the value of cls1 is always NULL. I think that JVM can find the JAR, because when I'm debugging I can't delete the JAR after FindClass.
The JAR file contains the MyClass.class file, I checked it.
I have checked some previous questions ( 1, 2, 3 and some others) yet, but I couldn't get where I made a mistake.
UPDATE: I tried to copy the MyClass.class file and MyJar.jar file into the directory of the example linked before, and JVM can't find MyClass. It's possible that something missing from my java source file? The package declaration is correct.
What can cause that JVM can't find MyClass?