0
在此篇How to use JNI to start JVM in builder C++ application我轉換jvm.lib從Coff的到OMF。我用它來連接JNI_CreateJavaVM()函數。但它崩潰了。使用JNI啓動JVM生成器C++
我使用的另一種代碼從生成器C++應用程序啓動JVM。當我加載jvm.dll。這行得通!! JVM是在我加載java目錄中的jvm時創建的,但是當我將此文件(jvm.dll)複製到另一個目錄並從此目錄調用它時,JVM就會創建。可以解釋爲什麼我不能使用jvm.dll的副本來創建我的JVM?
這是我的代碼:
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;
在這個崗位http://stackoverflow.com/questions/7989502/jni-createjavavm-exit-code-1並在ansewer N°1,他說,這是前人的精力在ProgramFile \ java的\導演JVM .. 1-將jvm.dll所在的位置(應該在程序文件\ java \ jsdk _ * \ bin \ server中)添加到Windows機器上的PATH環境變量中。 – BestHumain 2014-08-28 14:18:52