1

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!

Community
  • 1
  • 1
Charlie
  • 152
  • 1
  • 12

1 Answers1

1

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.

lesderid
  • 3,388
  • 8
  • 39
  • 65
  • Hi lesderid, thanks a lot for your answer. About the possible answers you, I'd like to try with the second one. However I installed all x86_64-w64-mingw32 related packages in cygwin but the gcc is still unable to generate the app in 64 bits. But I realized that if I use the /bin/x86_64-w64-mingw32-nm instead of the nm, the symbols for the jvm.lib are appearing, so I can guess /bin/x86_64-w64-mingw32-gcc should be able to generate a 64 bits app, but when I am trying it's unable to find some system headers. Do you know which is the normal/easy way to generate the 64 bits application in cygwin? – Charlie Oct 31 '11 at 11:55
  • Always use the whole toolchain. If you use x64 gcc, use x64 nm. About the system headers, could you post the exact errors? – lesderid Oct 31 '11 at 14:33
  • To build an x64 application with cygwin (mingw): http://stackoverflow.com/questions/873812/how-to-compile-existing-posix-code-for-64-bit-windows – lesderid Oct 31 '11 at 14:41