JNI_CreateJavaVM总是失败

时间:2012-07-09 05:28:44

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

我正在尝试通过Windows服务创建JVM,但JNI_CreateJavaVM始终失败。这是我的代码,

DWORD WINAPI InvokeMain(LPVOID lpParam) {
    // HACK to hang the function
    while(1) {
        if( (_access( "e:\\file", 0 )) != -1 ) {
            break;
        }
    }
    //Load the JVM Dll
    HINSTANCE ghDllInstance = LoadLibrary(L"E:\\jdk1.6.0_26\\jre\\bin\\server\\jvm.dll");

    if( ghDllInstance == 0) {
        Log("Failed to load jvm dll\n");
        return FALSE;
    }
    //resolve the function pointer JNI_CreateJVM
    CreateJavaVM createJVMStart = (CreateJavaVM)GetProcAddress(ghDllInstance, "JNI_CreateJavaVM");

    JavaVMOption options[2];
    options[0].optionString    = "-Djava.class.path=e:\\win\\product\\lib"; // application class path 
    options[1].optionString    = " start";

    JavaVMInitArgs vm_args;
    vm_args.version            = JNI_VERSION_1_6; //JNI Version 1.4 and above
    vm_args.options            = options;
    vm_args.nOptions           = 2;
    vm_args.ignoreUnrecognized = JNI_FALSE;


    //Create the JVM
    JavaVM *vmStart;
    JNIEnv *env;
    jint res = createJVMStart(&vmStart, (void **)&env, &vm_args);
    if (res < 0)  {
        Log( "Error creating JVM");
        return FALSE;
    }       
    return TRUE;
}

1 个答案:

答案 0 :(得分:2)

我从未使用过1.6 VM。但我很确定start不是一个有效的选项 由于您将JNI_FALSE传递给ignoreUnrecognized,因此不会创建VM 我想start是你的程序的一个参数。但是你需要自己将它传递给main函数。