【问题标题】:JNI_CreateJavaVM crashes C++ application without error messageJNI_CreateJavaVM 使 C++ 应用程序崩溃而没有错误消息
【发布时间】:2016-06-30 10:33:05
【问题描述】:

我想在 C++ 应用程序中使用 Voce。现在,在语音标题中,更具体地说,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)。但是,由于 Voce 需要该 JavaVM,因此这是一个问题。显然。我的猜测是,我需要一些 dll,目前我的 jvm.dll 与 main.cpp 位于同一文件夹中,但与 voce.h 不同。

【问题讨论】:

    标签: java c++ dll jvm voce


    【解决方案1】:

    如果您对已安装的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多