JNI FindClass fails with imports
I'm trying to call my java class from c++ (ms vc 2008) with JNI, but the problem is that FindClass works only when there are not imported packages in my java class. If I add any package (for example java.lang.String or java.io.File) the FindClass fails and returns no value. Why?
C++ CODE: /////////////////////////////////////
JNIEnv *env;
JavaVM * jvm;
JavaVMInitArgs vm_args;
JavaVMOption options;
options.optionString = "-Djava.class.path=c:\\mypackage.jar";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(env == NULL)
printf("\nJVM Failed\n");
jclass cls = env->FindClass("mypackage开发者_StackOverflow社区/test/MyClass");
if(cls==0)
{
printf("\nFindClass Failed\n");
}
JAVA CODE: /////////////////////////////////////
package mypackage.test;
import java.io.File; //if I comment this row, FindClass works...
public class MyClass {
}
UPDATE:
THIS IS MY CODE: http://www.sendspace.com/file/233tfm
- copy in C:\JNITest
- change the working directory in the properties\debug settings of the project
- check the optionString in JNI_test1.cpp
many thanks, Riccardo
The VM cannot find rt.jar and all the other classes. This question suggests to run JNI_GetDefaultJavaVMInitArgs(&vm_args);
before you set other options.
I guess it could be because your class path is only c:\mypackage.jar when you create the JVM. I think the system class path has to be specified. Class path can be set as options.optionString = "-Djava.class.path=c:\mypackage.jar;C:\jdk1.6.0_18\jre\lib\rt.jar"; Please change based on where rt.jar is on your system....
I have found the problem. If I select in Eclipse\Jar Export\ the option "Export generated class files and resources" AND I put some import in my java class, the generated JAR file will not include any .class file. Instead the option "Export all output folders for checked projects" works anytime. I don't know why, but I will investigate.
精彩评论