在调用DestroyJavaVM之后,重新调用JNI_CreateJavaVM会返回-1

时间:2013-08-28 11:22:54

标签: java c++ linux jvm java-native-interface

我试图在调用DestroyJavaVM之后回忆起JNI_CreateJavaVM。

我按照以下顺序调用它们:InitJVM - > ReleaseJVM - > InitJVM

首次初始化时,JNI_CreateJavaVM返回0并正常工作。 第二次在销毁后初始化时,JNI_CreateJavaVM返回-1并且不起作用。

会出现什么问题?

这是一个创建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<char*> ("-Xmx128m"), NULL},
        { const_cast<char*> ("-verbose:gc"), NULL},
        { const_cast<char*> ("-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<void**> (&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

这是删除JVM的函数:

void PWNJava::ReleaseJVM(void) {

    if (jvm)
        jvm->DestroyJavaVM();

    jvm = NULL;
    delete jvm;

    env = NULL;
    delete env;

    m_bRunning = false;

    Log("Success : PWNJava::ReleaseJVM");

}

1 个答案:

答案 0 :(得分:2)

上次我查看JNI_DestroyJVM()时,它说"The JDK/JRE still does not support VM unloading, however."只是不要打电话给它,也不要重新初始化它。