I am trying to recall JNI_CreateJavaVM after calling DestroyJavaVM.
I call them in following order: InitJVM -> ReleaseJVM -> InitJVM
First time when initialing, JNI_CreateJavaVM returns 0 and works fine.
second time when initialing after destroying, JNI_CreateJavaVM return -1 and does not work.
What would be the problem?
Here is a function that create JVM:
#elif __linux
HRESULT PWNJava::InitJVM(void) {
if(m_bRunning) {
Log("Error : JVM is already Running! (PWNJava::Init)");
return PWN_FAIL;
}
const int kNumOptions = 3;
JavaVMOption options[kNumOptions] = {
{ const_cast ("-Xmx128m"), NULL},
{ const_cast ("-verbose:gc"), NULL},
{ const_cast ("-Djava.class.path=../../Class"), NULL}
};
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = sizeof (options) / sizeof (JavaVMOption);
assert(vm_args.nOptions == kNumOptions);
int res = JNI_CreateJavaVM(&jvm, reinterpret_cast (&env), &vm_args);
if (res != JNI_OK) {
Log("Error : createJavaVM(&jvm, &env, &vm_args)");
return PWN_FAIL;
}
m_bRunning = true;
Log("Success : PWNJava::Init");
return PWN_OK;
}
#endif
Here is a function deleting JVM:
void PWNJava::ReleaseJVM(void) {
if (jvm)
jvm->DestroyJavaVM();
jvm = NULL;
delete jvm;
env = NULL;
delete env;
m_bRunning = false;
Log("Success : PWNJava::ReleaseJVM");
}
解决方案
Last time I looked at JNI_DestroyJVM(), it said "The JDK/JRE still does not support VM unloading, however." Just don't call it, and don't re-initialize it either.