如何使用JNI从C ++向CallObjectMethod传递多个参数

时间:2016-09-22 09:30:24

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

我正在尝试使用JNI将三个字符串传递给java静态方法,但我无法找到如何操作。我能够传递一个字符串作为输入,但不清楚如何传递多个参数。你能帮忙吗?

int _tmain(int argc, _TCHAR* argv[])
{
    JavaVM *vm;
    JNIEnv *env;
    JavaVMInitArgs vm_args;
    JavaVMOption options[1];
    vm_args.version = JNI_VERSION_1_2;
    vm_args.nOptions = 1;

    options[0].optionString = "-Djava.class.path=C:\\Algorithm\\output";
    vm_args.options = options;
    vm_args.ignoreUnrecognized = 1;

    // Construct a VM
    jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);

    jclass clazz_algo = env->FindClass("systems/DataExchange");
    if (env->ExceptionCheck()) {
        return 1;
    }

    // init(String, String, String)
    jmethodID initMethod = env->GetStaticMethodID(clazz_algo, "init", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");

    //HOW DO I CALL initMethod with three strings as input?

    // Shutdown the VM.
    vm->DestroyJavaVM();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

//HOW DO I CALL initMethod with three strings as input?
// Those are created as example
jstring str1=NewStringUTF(env, "Str1");
jstring str2=NewStringUTF(env, "Str2");
jstring str3=NewStringUTF(env, "Str3");

// C++ style
env->CallStaticVoidMethod(clazz_algo, initMethod, str1,str2,str3);
// ... or C-style
CallStaticVoidMethod(env, clazz_algo, initMethod, str1,str2,str3);