gpt4 book ai didi

java - 如果 jvm 的参数是从其他类型转换过来的,jni 找不到类

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:58:50 27 4
gpt4 key购买 nike

<分区>

我有一个关于 jni 和 c++/qt 的小问题:这是我的代码:

void Jvm::create_jvm(QStringList &arguments){
QTextStream cout(stdout);
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption *options[4];
//run begin
//options[0].optionString = "-Djava.compiler=NONE";
//options[1].optionString = "-Djava.class.path=app";
//options[2].optionString = "-verbose:jni";
//options[3].optionString = "-Xmx512m";
//run end

//can't find the class begin
QByteArray bArray;
cout<<"Valores del array arguments"<<endl;
for(int i;i<arguments.size();i++){

cout<<arguments.at(i)<<endl;
bArray=arguments.at(i).toStdString().c_str();
options[i].optionString = bArray.data();
cout<<options[i].optionString<<endl;
}
//can't find the class end


//jvm args
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 4;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_FALSE;


jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {//jvm don't created
qDebug()<<"Can't create Java VM\n";
exit(1);
}else{//jvm created
invoke_class(env);
jvm->DestroyJavaVM();
}
}

void Jvm::invoke_class(JNIEnv* env) {
jclass cls;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0,applicationArg1,applicationArg2;

//class and main method
cls = env->FindClass("HolaMundoSwing");
if (cls == 0) qDebug()<<"Sorry, I can't find the class"; //In case that class not exist
mainMethod = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");

//Application arguments array
applicationArgs = env->NewObjectArray(3, env->FindClass("java/lang/String"), NULL);
applicationArg0 = env->NewStringUTF("From");
applicationArg1 = env->NewStringUTF("C");
applicationArg2 = env->NewStringUTF("program");

env->SetObjectArrayElement(applicationArgs, 0, applicationArg0);
env->SetObjectArrayElement(applicationArgs, 1, applicationArg1);
env->SetObjectArrayElement(applicationArgs, 2, applicationArg2);

env->CallStaticVoidMethod(cls, mainMethod, applicationArgs); //Call to the method
}

我测试了参数,都是一样的。类路径具有相同的值,但注释代码运行正确,其他代码找不到该类,知道吗?

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