JNI_CreateJavaVM使c++应用程序崩溃,没有错误消息

JNI_CreateJavaVM crashes C++ application without error message

本文关键字:有错误 消息 崩溃 应用程序 CreateJavaVM c++ JNI      更新时间:2023-10-16

我想在c++应用程序中使用voice。现在,在voice header中,更具体地说是void init()中有这样一个部分:

vm_args.nOptions = 2;
// Setup the VM options.
// TODO: check out other options to be used here, like disabling the 
// JIT compiler.
JavaVMOption options[2];
// Add the required Java class paths.
std::string classPathString = "-Djava.class.path=";
classPathString += vocePath;
classPathString += "/voce.jar";
char s[1024];
sprintf(s, classPathString.c_str());
options[0].optionString = s;
options[0].extraInfo = NULL;
// Add an option to increase the max heap size. (1)
char x[1024] = "-Xmx256m";
options[1].optionString = x;
options[1].extraInfo = NULL;
//options[1].optionString = "-Djava.compiler=NONE"; // Disable JIT.
//options[1].optionString = "-verbose:gc,class,jni";
vm_args.options = options;
//vm_args.ignoreUnrecognized = JNI_FALSE;
// Create the VM. (2)
status = JNI_CreateJavaVM(&internal::gJVM, (void**)&internal::gEnv, &vm_args);

如果我尝试像这样运行它,它会崩溃。如果我将所有内容放在(1)之后的注释中,它就会运行。之后,如果我将JNI_CreateJavaVM放入注释(2)中,它也可以运行。如果我直接从崩溃跳转到在注释中放入(2),它又会崩溃。我需要在(1)和(2)上取路径,然后只取(2)。然而,由于voice需要那个JavaVM,这是一个问题。显然。我的猜测是,我需要一些dll,目前我有jvm.dll在同一文件夹中的main.cpp,但不像voice .h.

如果对安装的jvm.dll使用动态链接和加载,应该可以正常工作。

typedef jint(JNICALL *pCreateJavaVM)(JavaVM **, void**, void *);
HINSTANCE hInstance = LoadLibrary(L"C:\Program Files\Java\jre1.8.0_77\bin\server\jvm.dll");
pCreateJavaVM CreateJavaVM = (pCreateJavaVM)GetProcAddress(hInstance, "JNI_CreateJavaVM");
jint res = CreateJavaVM(&vm, (void **)&env, &vm_args);