I am having a similar issue to JNI_CreateJavaVM exit code -1?, except that my code runs when compiled in 32-bit, but fails when compiled in 64-bit. I am changing my JDK location for each configuration in Visual Studio Express 2010, using the Windows SDK 7.1 platform. My includes are:
C:\Program Files\Java\jdk1.6.0_29\include; (for jni.h)
C:\Program Files\Java\jdk1.6.0_29\include\win32; (for jni_md.h)
and my additional libraries are:
C:\Program Files\Java\jdk1.6.0_29\lib; (for jvm.lib and jawt.lib)
My source code is just simply trying to initialize the JVM in C and then looping infinitely to make sure that the command prompt stays open in VSC++.
#include "stdafx.h"
#include "jni.h"
JNIEnv* create_vm(JavaVM ** jvm);
int i;
JavaVM* jvm;
JNIEnv * env;
int _tmain(int argc, _TCHAR* argv[])
{
printf("Hello World!");
env = create_vm(&jvm);
if (env == 0) { return 7; }
i = 0;
while (1) { i++; }
return 0;
}
JNIEnv* create_vm(JavaVM ** jvm) {
JavaVMInitArgs vm_args;
int ret;
JavaVMOption options;
//Path to the java source code
options.optionString = "-Djava.class.path=H:\\jarpath\\jarfile.jar";
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
printf("\nUnable to Launch JVM\n");
return env;
}
Again, this works with a 32-bit configuration, where the only difference is that the JDK root directory is a 32-bit folder (i.e. Program Files(x86)).
After printing,
Error occurred during initialization of VM
Unable to load native library: Can't find dependent libraries
The code exits with the following error in the VSC++ console window:
The program '[5684] JNI_Test.exe: Native' has exited with code 1 (0x1).
Is there anything I’m missing in terms of libraries or basic C coding? I haven’t coded in C in a while and I’m not very familiar with the whole library linking in the first place.
Try this code (it does not use the export .lib files and it only needs the JNI headers and JVM.dll). I’ve used the command
to compile it (MinGW, but VC++ should work also. Mingw uses older MSVCRT)
Check for GetLastError(). I suspect there’s something wrong with the 64-bit C Run-Time.