如何从 C++ 生成 Java VM在 64 位 Windows 7 上使用 cygwin?
我正在尝试从 C++ 程序创建 Java 虚拟机 (JVM)。经过研究,我发现我需要调用 JNI_CreateJavaVM 方法来实现它。只是为了尝试,我得到了 Michael Bruckmeier 在这个问题中发布的代码 它不会'不要创建 Java VM (JNI),只需更改很少的内容即可避免警告。
#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;
}
我在 cygwin 内的 gcc 中编译了前面的程序,但出现了一些链接错误:
$ 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
为了检查 jvm.lib 中的符号,我使用 nm 命令,并且得到了一个非常大的这些消息列表:
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
我可以猜测问题是Java 开发工具包 (JVM) 是 64 位的。我的操作系统是 64 位 Windows 7,gcc 正在生成 32 位应用程序。所以,我认为存在不兼容性。我还尝试生成 64 位应用程序(尽管我更喜欢 32 位应用程序),结果如下:
$ 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
有人可以建议一种使用此环境在 C++ 中实现 JVM 的方法吗?或者如果我错了(也可能是这样),有人可以告诉我为什么会出现这些错误吗?
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0x8664 是 DLL 的 COFF 标头中的 IMAGE_FILE_MACHINE_AMD64(其描述为“x64”)常量。所以基本上 GCC 确实表示它不支持 x64 DLL。
关于第二部分,经过一番搜索后,我发现当您的编译器未使用 x86-x64 支持进行编译时,您会收到此错误消息(“抱歉,未实现:未编译为 64 位模式”)。
有两种可能的解决方案:一是切换到 x86 DLL/JDK。或者两个,为 Cygwin 编译 GCC,并支持 x86-x64。
所以简短的回答是:不,在您当前的环境中不可能做到这一点。
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.