C++ JNI:VM初始化期间发生错误无法设置启动类路径

问题描述 投票:0回答:1

这是我的工作空间。

ls
jvm.dll   main.cpp  main.exe  main.obj

这是我的代码

#include <jni.h> /* where everything is defined */

int main()
{
    JavaVM* jvm; /* denotes a Java VM */
    JNIEnv* env; /* pointer to native method interface */
    JavaVMInitArgs vm_args; /* JDK/JRE 19 VM initialization arguments */
    JavaVMOption* options = new JavaVMOption[1];
    options[0].optionString = "-Djava.class.path=C:\\Program Files\\Zulu\\zulu-21\\lib";
    vm_args.version = JNI_VERSION_19;
    vm_args.nOptions = 1;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;
    /* load and initialize a Java VM, return a JNI interface
     * pointer in env */
    JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    delete options;
    /* We are done. */
    jvm->DestroyJavaVM();
}

这是我的构建任务:

 cl.exe C:\Users\AB\Desktop\tmp\whylinkerr\main.cpp /I "C:\Program Files\Zulu\zulu-21\include" /I "C:\Program Files\Zulu\zulu-21\include\win32" /link "C:\Program Files\Zulu\zulu-21\lib\jvm.lib"

当我运行 main.exe 时:

➜ .\main.exe
Error occurred during initialization of VM
Failed setting boot class path.

行不通。

我试过了

    options[0].optionString = "-Xbootclasspath/a:C:\\Program Files\\Zulu\\zulu-21;";
但它仍然说无法设置引导类路径。

我的代码是从官网复制的https://docs.oracle.com/en/java/javase/21/docs/specs/jni/invocable.html

c++ java-native-interface
1个回答
0
投票

我找到了另一个解决方案。就在工作区

rm jvm.dll
, 然后


typedef int (__stdcall * JNI_CreateJavaVMFunc)(JavaVM** pvm, JNIEnv** penv, void* args);

HINSTANCE jvmDLL = LoadLibraryA("C:\\Program Files\\Zulu\\zulu-17\\bin\\server\\jvm.dll");

JNI_CreateJavaVMFunc Dll_JNI_CreateJavaVM = (JNI_CreateJavaVMFunc)GetProcAddress(jvmDLL, "JNI_CreateJavaVM");
JavaVM* jvm = nullptr;
JNIEnv* env = nullptr;
JavaVMInitArgs vm_args; /* JDK/JRE 19 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
char arg[50] = "-verbose:jni";
options[0].optionString = arg;
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = true;

auto check = Dll_JNI_CreateJavaVM(&jvm, & env, &vm_args);

然后一切正常...

不要在jni.h中使用JNI_CreateJavaVM

我从jni-sys

找到了这个解决方案
© www.soinside.com 2019 - 2024. All rights reserved.