gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:14:41 26 4
gpt4 key购买 nike

我正在尝试使用 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;
}

最佳答案

//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);

关于java - 如何使用 JNI 从 C++ 向 CallObjectMethod 传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39635274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com