2012-09-21 50 views
1

我在PC上安裝了幾個Java安裝程序。有沒有可能通過設置jvm.dll應該加載哪個路徑?下面的代碼創建JVM使用JNI:JNI通過選擇要加載的jvm.dll來創建JVM

void createJVM() 
{ 

JavaVMInitArgs vm_args; 
JavaVMOption options[4]; 

int n = 0;  
char * str; 
str= new char[1000]; 
sprintf(str, "-Djava.class.path=%S\\tst.jar", myPath); 
options[n++].optionString = str; 

str= new char[1000]; 
sprintf(str, "-Djava.library.path=%S\\lib;%S", myPath, myPath); 
options[n++].optionString = str; 

str= new char[1000]; 
sprintf(str, "-Duser.dir=%S", myPath); 
options[n++].optionString = str; 

vm_args.version = JNI_VERSION_1_4; 
vm_args.nOptions = n;  
vm_args.options = options;  
vm_args.ignoreUnrecognized = false; 


JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); 
} 

回答

5

有2個選項我所知道的:第一種是設置PATH環境變量JVM.DLL目錄,你想運行的可執行文件和第二前使用是在運行時動態鏈接到jvm.dll,例如通過使用LoadLibrary

HINSTANCE hinstLib = LoadLibrary(TEXT("D:\\desired_jvm\\jre6\\bin\\client\\jvm.dll")); 
typedef jint (JNICALL *PtrCreateJavaVM)(JavaVM **, void **, void *); 
PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)GetProcAddress(hinstLib,"JNI_CreateJavaVM"); 
jint res = ptrCreateJavaVM(&jvm, (void**)&env, &vm_args); 
//... 
相關問題