[英]JNI FindClass can't find class which uses jar
I'm working on a project where some Java functions must be called from C++ code using JNI. 我正在开发一个项目,其中必须使用JNI从C ++代码调用某些Java函数。 I've tried that with a simple Java class, but when I'm starting to use extra
.jar
in my Java project JNI's FindClass
function can't find my class. 我试过一个简单的Java类,但是当我开始在我的Java项目中使用额外的
.jar
,JNI的FindClass
函数找不到我的类。 I've done some research and read about classpath
which is needed for compiling .java file if it uses extra libs, but FindClass
returns null
in that case. 我做了一些研究并阅读了关于编译.java文件所需的
classpath
,如果它使用额外的libs,但在这种情况下FindClass
返回null
。 Here's basic structure of my code 这是我的代码的基本结构
JavaVMOption options[2];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean not;
options[0].optionString = "-Djava.class.path=<path_to_my_java_class>";
options[1].optionString = "-Djava.library.path=<path_to_my_jar_file>";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 2;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (status != JNI_ERR)
{
cls = env->FindClass("package/ClassName"); //returns null while using jar
if(cls != 0)
{
//do some stuff
}
jvm->DestroyJavaVM();
return 0;
}
else
return -1;
Any ideas? 有任何想法吗?
UPDATED: I've also tried 更新:我也试过了
options[0].optionString = "-Djava.class.path=<path_to_my_java_class>;<path_to_jar>";
options[0].optionString = "-Djava.class.path=<path_to_my_java_class>";
options[1].optionString = "-classpath <path_to_jar>";
I think that your mistake is that you are putting a JAR on the "library.path". 我认为你的错误是你在“library.path”上放了一个JAR。 The library path is the path for finding native libraries ... not JAR files.
库路径是查找本机库的路径...而不是JAR文件。
You should put the JAR file on the classpath; 您应该将JAR文件放在类路径上; eg
例如
options[0].optionString =
"-Djava.class.path=<path_to_my_java_class>:<path_to_my_jar_file>";
(On Windows, use ";" as the classpath separator instead of ":".) (在Windows上,使用“;”作为类路径分隔符而不是“:”。)
I figured out, that there were 2 problems 我发现,有2个问题
1) path_to_my_jar_file 1)path_to_my_jar_file
This path must point to the jar file and not to the directory folder which contains it. 此路径必须指向jar文件,而不是指向包含它的目录文件夹。
2) -Djava.library.path 2)-Djava.library.path
Comments of Drew McGowen and answer of Stephen C were right - path to jar file must be like they said. Drew McGowen的评论和Stephen C的答案是正确的 - jar文件的路径必须像他们所说的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.