Search code examples
javajvmjava-native-interfaceadoptopenjdk

JNI_CreateJavaVM fails with Adopt OpenJDK 11.0.8


Our 64 bit C program instantiates a JVM using JNI. Currently we are using Adopt Open JDK 8.0.1920.12 and everything works fine. Now we are trying to migrate to Adopt Open JDK 11.0.8.0. When using that one, JNI_CreateJavaVM always returns -1.

What I did is basically a) download the JDK (file OpenJDK11U-jre_x64_windows_hotspot_11.0.8_10.zip) b) put its content into the folder that previously contained the Java 8 JDK. This folder can be found in the PATH environment variable

Our code is:

char* ipV4 = "-Djava.net.preferIPv4Stack=true";
opts[numopts++].optionString = ipV4;

// get path pointing to our Java classes
classpath = WfBuildJVMClasspathStr();
opts[numopts++].optionString = classpath;

//make sure we notify the JVM we are a service
sprintf(servicearg, "-Xrs");
opts[numopts++].optionString = servicearg;

vm_args.version = JNI_VERSION_1_2;
vm_args.options = opts;
vm_args.nOptions = numopts;
vm_args.ignoreUnrecognized = true;

status = JNI_CreateJavaVM(&jvm, (void **)&envP, &vm_args);

Alternatively I also tried with replacing the last line with the following code:

char JVMExe[STRPATH + 1];
// make up path to the <JDK install>\bin\server\jvm.dll
sprintf(JVMExe, "%s\\jvm.dll", WF.JVMDir);
HINSTANCE hinstLib = LoadLibrary(TEXT(JVMExe));
typedef jint(JNICALL *PtrCreateJavaVM)(JavaVM **, void **, void *);
PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)GetProcAddress(hinstLib, "JNI_CreateJavaVM");
status = ptrCreateJavaVM(&jvm, (void**)&envP, &vm_args);

Unfortunately the same (negative) result.

Any idea what I am doing wrong?


Solution

  • I found the cause of my problem. I created a minimal command line exe and let it write to the console. I got this error message: <JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; Use -classpath instead.

    When looking for that I found articles like this: https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-2C896CA8-927C-4381-A737-B1D81D964B7B

    After renaming the "ext" folder in the "lib" dir of my JRE to e.g. "ext_go_away" JNI_CreateJavaVM succeeded.