use JNI to start JVM Builder C++

强颜欢笑 提交于 2020-01-06 18:57:06

问题


in this post How to use JNI to start JVM in builder C++ application i converted jvm.lib from Coff to Omf. i used it to link JNI_CreateJavaVM() function. But it crash .

I used another code to start JVM from Builder C++ application. When i load the jvm.dll. It work!! the JVM is created when i load jvm in java directory but not when i copy this file(jvm.dll) in another directory and call it from this directory.Can you explain why i can't use a copy of jvm.dll to creat my JVM?

This is my Code :

JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
memset(&vm_args, 0, sizeof(vm_args));

options.optionString = "-Djava.class.path=.;tika-app-1.5.jar;";
jint ii = GetVersion();

vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
//String strPath = "C:\\Users\\mhechkel\\Documents\\RAD Studio\\Projects\\TestJVM\\Win32\\Debug\\jvm.dll";  
// when i copy jvm.dll in my application folder: It dosn't work!!!
String strPath = "C:\\Program Files (x86)\\Java\\jdk1.7.0_65\\jre\\bin\\server\\jvm.dll";

HMODULE jvm_dll = LoadLibrary(strPath.c_str()); //here it work fine!!

/// You might check the GetLastError() here after the LoadLibrary()
if(jvm_dll == NULL) 
{    
    Label1->Caption = "can't load dll"; 
}

 JNI_CreateJavaVM_ptr = (JNI_CreateJavaVM_func)GetProcAddress(jvm_dll, "JNI_CreateJavaVM");

 /// You might check the GetLastError() here
 if(JNI_CreateJavaVM_ptr == NULL)
 {
      Label1->Caption = "can't load function";
 }

 int ret = JNI_CreateJavaVM_ptr(jvm, (void**)&env, &vm_args);
 if(ret < 0)
 {
    Label1->Caption = "Unable to Launch JVM\n";
 }
 return env;

回答1:


You'll need the complete JRE in order to create Java VM.
JVM will not start if does not find matching JRE at the place relative to jvm.dll path.

On Windows you can override the path where JVM looks for JRE using _ALT_JAVA_HOME_DIR environment variable, though this is a HotSpot internal option and is not guaranteed to work.



来源:https://stackoverflow.com/questions/25550578/use-jni-to-start-jvm-builder-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!