Reputation: 1021
I'm trying JNI with C++. But I get this error: Class not found!
. I searched other solved, but not working for me.
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;
}
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?
Upvotes: 2
Views: 4263
Reputation: 3462
I initially put this in a comment, but poster indicates it is the solution.
Your code assumes that "myclasses" is a folder under the CWD of your C++ program. While the directory structure looks correct, this won't work unless the CWD is the parent of "myclasses". Try printing the return of getcwd() and see where you are
Upvotes: 2