这是C ++中的代码,我创建了一个Java VM,我需要从jar调用一个函数。
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
jmethodID constr = NULL;
jmethodID Read_XML = NULL;
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=<path_to_my_jar>";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
long status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (status == JNI_ERR){
cout << "Fail: Unable to load JVM \t Exit" << endl;
}
jclass xml_read = env->FindClass("<MyClass>");
if (env->ExceptionCheck()){
cout << "Fail:";
}
constr = env->GetMethodID(xml_read, "<init>", "()V");
if (env->ExceptionCheck()){
cout << "Fail:";
}
Read_XML = env->GetStaticMethodID(xml_read,"readFromXML", "(Ljava/lang/String;)L<MyClass>;");
if (env->ExceptionCheck()){
cout << "Fail:";
}
const char* filepath = "<My_filepath>";
const jstring file = env->NewStringUTF(filepath);
jobject ret_obj = env->CallStaticObjectMethod(xml_read,Read_XML,file);
if (env->ExceptionCheck()){
cout << "Fail:";
}
现在ret_obj为NULL。我调用的Java函数接受一个String作为参数,并从另一个不同于MyClass的类返回一个Object。
Java方法就像这样public static SomeClass readFromXML(String filepath)
更新1
在CallStaticObjectMethod中,我们有jobject CallStaticObjectMethod(jclass clazz, jmethodID methodID,
...) {
va_list args;
jobject result;
va_start(args,methodID);
result = functions->CallStaticObjectMethodV(this,clazz,methodID,args);
va_end(args);
return result;
}
在va_start(args,methodID);
之后,args的值是这样的事情0x002cf5fc "0¦ύ"
所以也许这些论点有些东西?
答案 0 :(得分:0)