JNI通过选择要加载的jvm.dll来创建JVM

时间:2012-09-21 09:50:40

标签: visual-c++

我的PC上安装了几个Java。有可能以某种方式传递应该加载jvm.dll路径的设置吗?以下代码使用JNI创建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); 
}

1 个答案:

答案 0 :(得分:6)

我知道有两个选项:第一个是在运行可执行文件之前将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);
//...