我正在编写一个程序,我使用 JNI 从 C++ 代码调用 Java 函数。如果我.class
直接使用文件,我的程序运行良好,但是当我将它添加到 jar 文件时FindClass
失败。例如,这很好用
std::string stdOpt = "-Djava.class.path=<path_to_jar>/dependency.jar;<path_to_class>";
boost::scoped_array<char> opt(new char[stdOpt.size() + 1]);
std::copy(stdOpt.begin(), stdOpt.end(), opt.get());
opt[stdOpt.size()] = '\0';
options[0].optionString = opt.get();
JavaVMInitArgs vm_args;
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
long status = JNI_CreateJavaVM(&m_pJavaVirtualMachine, (void**)&m_pJavaEnvironment, &vm_args);
if (status != JNI_OK)
throw std::logic_error("Cannot start Java Virtual Machine");
m_class = m_pJavaEnvironment->FindClass("MyClass");
if(m_class == 0)
throw std::logic_error("Cannot find Java class");
但是这个失败了
std::string stdOpt = "-Djava.class.path=<path_to_jar>/dependency.jar;<path_to_jar>/myjar.jar";
boost::scoped_array<char> opt(new char[stdOpt.size() + 1]);
std::copy(stdOpt.begin(), stdOpt.end(), opt.get());
opt[stdOpt.size()] = '\0';
options[0].optionString = opt.get();
JavaVMInitArgs vm_args;
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
long status = JNI_CreateJavaVM(&m_pJavaVirtualMachine, (void**)&m_pJavaEnvironment, &vm_args);
if (status != JNI_OK)
throw std::logic_error("Cannot start Java Virtual Machine");
m_class = m_pJavaEnvironment->FindClass("MyClass"); //m_class=NULL
if(m_class == 0)
throw std::logic_error("Cannot find Java class"); //throwing exception
当我尝试从中获取 Java 类时,我应该做些什么.jar
?怎么了?有任何想法吗?