Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

c++ - JNI_CreateJavaVM() terminates with exit code 1

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_51libjvm.lib, including jdk1.7.0_51include and jdk1.7.0_51includewin32. 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;
}

OS: Windows 7 (x64)

Compiler: Visual Studio 2012 (x86/Win32 Project)

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)

I've already tried to using both: jdk1.7.0_51jreinclientjvm.dll as well as jdk1.7.0_51jreinServerjvm.dll - with the same result (I'm not entirely sure what the difference is though).

Any ideas & suggestions would be highly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using static linking

  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.

Using dynamic linking.

  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)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...