简体   繁体   English

从Win32 / MFC应用程序中启用后无法使用JNI

[英]Unable to use JNI when enabled from within Win32/MFC application

I'm trying to use Java functions from within a Visual C++ application by availing of the Java Native Interface, but am not having much luck. 我试图通过Java本机接口从Visual C ++应用程序中使用Java函数,但是运气不佳。

I'm calling the code in a button click handler function within a CDialog, and I get as far as trying to create a Java VM instance and my application throws an access violation exception: "Unhandled exception at 0x0F7D260A (mfc120u.dll) in MyApp.exe: 0xC0000005: Access violation reading location 0xFEEEFF26." 我在CDialog中的按钮单击处理程序函数中调用代码,并且尝试创建Java VM实例,并且我的应用程序抛出访问冲突异常:“ MyApp中0x0F7D260A(mfc120u.dll)的未处理异常.exe:0xC0000005:访问冲突读取位置0xFEEEFF26。 However, when I put my code into a skeleton C++ app containing just a main() function, it all works fine. 但是,当我将代码放入仅包含main()函数的C ++骨架应用程序时,一切正常。

As far as I can tell, I've set my project up correctly in Visual Studio 2013. I'm including jni.h from the include directory in my JDK folder, and I'm linking against jvm.lib contained inside my JDK lib folder. 据我所知,我已经在Visual Studio 2013中正确设置了我的项目。我从JDK文件夹的include目录中包含jni.h,并且链接到JDK lib中包含的jvm.lib夹。 I'm using JDK 1.8.0_91 as well. 我也在使用JDK 1.8.0_91。

For what it's worth, here is the code: 对于它的价值,这里是代码:

JNIEnv *env = NULL;
JavaVM *jvm = NULL;
jint res;
  // Initialization arguments
JavaVMInitArgs vm_args;
memset(&vm_args, 0, sizeof(vm_args));

JavaVMOption options[3];
memset(&options, 0, sizeof(options));

options[0].optionString = "-Djava.class.path=C:/";
options[1].optionString = "-Xms128m"; // 128MB initial heap size
options[2].optionString = "-Xmx1g"; // 1GB maximum heap size

vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
jint status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

Any help would be most appreciated 非常感激任何的帮助

In the end, I figured this out. 最后,我弄清楚了。 Apparently the JNI throws exceptions on startup by design to verify the environment it's running in. See Exception 0xC0000005 from JNI_CreateJavaVM (jvm.dll) . 显然,JNI在设计时会在启动时引发异常,以验证其运行所在的环境。请参见JNI_CreateJavaVM(jvm.dll)中的异常0xC0000005

When I disabled the particular exception in Win32 exceptions in visual studio , it all works fine. 当我在Visual Studio中的Win32异常中禁用特定异常时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM