First run main.exe it said can not find jvm.dll
So I copy jvm.dll from C:\Program Files\Zulu\zulu-21\bin\server
to ./
Then this is my workspace.
ls
jvm.dll main.cpp main.exe main.obj
But second run it said
Error occurred during initialization of VM
Failed setting boot class path.
It still can't work with jvm.dll.
This is my code
#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();
}
This is my build task:
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"
I tried options[0].optionString = "-Xbootclasspath/a:C:\\Program Files\\Zulu\\zulu-21;";
But it still say Failed setting boot class path.
My code was copied from the official website https://docs.oracle.com/en/java/javase/21/docs/specs/jni/invocation.html
Do not need jvm.dll in workspace. All I have to do is edit Path.
$env:path = "C:\Program Files\Zulu\zulu-21\bin\server;$env:Path"
I find another solution. Just rm jvm.dll
in workspace,
then
typedef int (__stdcall * JNI_CreateJavaVMFunc)(JavaVM** pvm, JNIEnv** penv, void* args);
HINSTANCE jvmDLL = LoadLibraryA("C:\\Program Files\\Zulu\\zulu-21\\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);
Then everything works fine...
Do not use JNI_CreateJavaVM in jni.h
I found this solution from jni-sys