JNI_CreateJavaVM()以退出代码1终止 [英] JNI_CreateJavaVM() terminates with exit code 1

查看:576
本文介绍了JNI_CreateJavaVM()以退出代码1终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JNI从C ++调用Java方法.为此,我安装了jdk1.7.0_51,并链接到jdk1.7.0_51\lib\jvm.lib,包括jdk1.7.0_51\includejdk1.7.0_51\include\win32.在Visual Studio 2012中使用以下代码,我试图创建一个Java vm对象-但是该函数始终以退出代码1终止我的应用程序(该函数不返回1:我的程序完全终止并发送退出代码1)./p>

I'm trying to call a Java method from C++ using JNI. To do that I've installed jdk1.7.0_51, linking against jdk1.7.0_51\lib\jvm.lib, including jdk1.7.0_51\include and jdk1.7.0_51\include\win32. using the following code in Visual Studio 2012 I tried to create a Java vm object - but the function always terminates my application with exit code 1 (the function doesn't return 1: my program terminates completly and sends the exit code 1).

#include <iostream>
#include "jni.h"

int main(int argc, char*argv[]){
  JNIEnv* env = nullptr;
  JavaVM* jvm = nullptr;
  JavaVMInitArgs vm_args;
  JavaVMOption options[2];
  options[0].optionString = "-Djava.class.path=.";
  options[1].optionString = "-DXcheck:jni:pedantic";  
  vm_args.version = JNI_VERSION_1_6;
  vm_args.nOptions = 2;
  vm_args.options = options;
  vm_args.ignoreUnrecognized = JNI_TRUE; // remove unrecognized options
  int ret = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
  std::cout << "This code is never reached" << std::endl;
  return 0;
}

操作系统: Windows 7(x64)

OS: Windows 7 (x64)

编译器: Visual Studio 2012(x86/Win32项目)

Compiler: Visual Studio 2012 (x86/Win32 Project)

Java虚拟机: jdk1.7.0_51,i586(我认为应该没问题,因为我正在为x86进行编译-否则无法与jvm.lib链接)

Java VM: jdk1.7.0_51, i586 (should be ok in my opinion, because I'm compiling for x86 - otherwise linkage with jvm.lib wouldn't work)

我已经尝试同时使用:jdk1.7.0_51\jre\bin\client\jvm.dlljdk1.7.0_51\jre\bin\Server\jvm.dll-具有相同的结果(我不确定是什么区别).

I've already tried to using both: jdk1.7.0_51\jre\bin\client\jvm.dll as well as jdk1.7.0_51\jre\bin\Server\jvm.dll - with the same result (I'm not entirely sure what the difference is though).

任何想法和建议将不胜感激.

Any ideas & suggestions would be highly appreciated.

推荐答案

使用静态链接

  1. 从项目目录中删除jvm.dll.该dll必须从其原始位置加载,因为似乎还涉及其他DLL,这些都是通过引用找到的.
  2. PATH环境变量设置为以JRE jvm.dll的文件夹开头.并且不要使用"c:\folder with space in name"表示法(用double quotes包围路径).只需使用set path=c:\folder with space in name;%PATH%.这个错误使我以前的尝试毫无价值.
  1. remove the jvm.dll from your project directories. The dll must be loaded from it's original location, as it seems that other DLLs are involved, found by references.
  2. Set the PATH environement variable to start with the folder of a JRE jvm.dll. And don't use the "c:\folder with space in name" notation (that is surrounding the path with double quotes). Just use set path=c:\folder with space in name;%PATH%. That mistake made my previous attempts worthless.

使用动态链接.

  1. 从项目目录中删除jvm.dll.该dll必须从其原始位置加载,因为似乎还涉及其他DLL,这些都是通过引用找到的.
  2. 从项目配置中删除jvm.lib
  3. 使用LoadLibrary,并带有jvm.dll的完整路径(转义'\'或使用'/')
  4. GetProcAddress用作"JNI_CreateJavaVM"
  5. 确保对函数指针使用正确的typedef(使用JNICALL作为调用约定)
  1. remove the jvm.dll from your project directories. The dll must be loaded from it's original location, as it seems that other DLLs are involved, found by references.
  2. Drop jvm.lib from your project configuration
  3. Use LoadLibrary, with the full path for jvm.dll (escape '\' or use '/')
  4. Use GetProcAddress for "JNI_CreateJavaVM"
  5. Make sure to use a proper typedef for the function pointer (use JNICALL as calling convention)

通过上述步骤对代码进行修补,使我的VS2012/Seven64/x86Debug/JDK1.6项目输出此代码从未到达"(使用ret == JNI_OK)

Patching your code with above steps makes my VS2012/Seven64/x86Debug/JDK1.6 project to output "This code is never reached" (with ret == JNI_OK)

这篇关于JNI_CreateJavaVM()以退出代码1终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆