如何在JNI中获取默认JVM INITIAL ARGS

时间:2010-01-25 15:14:07

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

我正在尝试获取可用JVM的默认jvm args,但我得到一个奇怪的输出。任何人都可以指出我的错误?

输出:

65542
�p����Y����k�.L�R���g���J����sk��,��*�Jk��xk��

代码:

#include "jni.h"
#include <iostream>
#include <dlfcn.h>
#include <cstdlib>

using namespace std;

void * JNI_FindCreateJavaVM(char *vmlibpath) {
    void *libVM = dlopen(vmlibpath, RTLD_LAZY);

    if (libVM == NULL) {
        return NULL;
    }
    return dlsym(libVM, "JNI_GetDefaultJavaVMInitArgs");
}

int main() {
    JavaVMOption vm_options;
    JavaVMInitArgs vm_args;

    vm_args.version = JNI_VERSION_1_6;
    vm_args.ignoreUnrecognized = JNI_FALSE;
    vm_args.options = &vm_options;

    void* (*lib_func)(void *) = 0;

    lib_func = (void*(*)(void*)) JNI_FindCreateJavaVM(
            "/usr/lib/jvm/java-6-sun/jre/lib/i386/client/libjvm.so");

    lib_func(&vm_args);

    cout << vm_args.version << endl;
    cout << vm_args.options[0].optionString << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

JNI_GetCreatedJavaVMs的原型是:

jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs);

您使用*JavaVMInitArgs参数调用该函数,我不确定您希望您的代码打印任何合理的代码。