Reputation: 152
I am trying to create a Java virtual machine (JVM) from a C++ program. After researching I found that I need to call JNI_CreateJavaVM method to achieve it. Just to try I got the piece of code Michael Bruckmeier posted in this question It won't create a Java VM (JNI) changing very few things to avoid warnings.
#include <jni.h>
#include <iostream>
int main(int argc, char *argv[])
{
char optionStr[] = "-Djava.class.path=./build/java"; //Path to the java source code
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = optionStr;
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = 0;
jint ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
std::cout << "JNI_CreateJavaVM returned " << ret << std::endl;
return 0;
}
I compile the previous program in gcc inside cygwin but I am getting some link errors:
$ gcc main.cpp /cygdrive/c/Progra~1/Java/jdk1.6.0_24/lib/jvm.lib -lstdc++ -o main
/tmp/ccKyd2Xk.o:main.cpp:(.text+0xfa): undefined reference to `_JNI_CreateJavaVM'
collect2: ld returned 1 exit status
In order to check the symbol in the jvm.lib I used nm command and I got a very large list of these messages:
BFD: /cygdrive/c/Progra~1/Java/jdk1.6.0_24/lib/jvm.lib(jvm.dll): Recognised but
unhandled machine type (0x8664) in Import Library Format archive
nm: jvm.dll: File format not recognized
I can guess the problem is that the Java Development Kit (JVM) is a 64-bit one. My OS is a 64-bit Windows 7, and the gcc is generating a 32-bit application. So, I think there is the incompatibility. I tried also to generate the app in 64-bit (although I would prefer having a 32-bit one) and this is the result:
$ gcc -m64 main.cpp /cygdrive/c/Progra~1/Java/jdk1.6.0_24/lib/jvm.lib -lstdc++ -o main
main.cpp:1: sorry, unimplemented: 64-bit mode not compiled in
Can some one suggest a way to achiving having a JVM in C++ using this environment? Or in case I am wrong (that could be as well), could someone tell me why I am getting these errors?
Thanks in advance!
Upvotes: 1
Views: 2422
Reputation: 3430
The 0x8664 is the IMAGE_FILE_MACHINE_AMD64 (its description is "x64") constant in the COFF header of the DLL. So basically GCC is indeed saying it doesn't support x64 DLLs.
About the second part, after searching a bit, I found that you get this error message ("sorry, unimplemented: 64-bit mode not compiled in") when your compiler isn't compiled with x86-x64 support.
Two solutions are possible: One, switching to an x86 DLL/JDK. Or two, compiling GCC for Cygwin with x86-x64 support.
So the short answer is: No, it is not possible to do it with your current environment.
Upvotes: 1