这是我的 main.cpp 代码:
#include <jni.h>
#include <QDebug>
int main(int argc, char *argv[]) {
JavaVM *jvm;
JNIEnv *env;
jstring jstr;
jobjectArray args;
jclass stringClass;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.classpath=.";
options[2].optionString = "";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {
qDebug()<<"Can't create Java VM\n";
exit(1);
};
jclass cls = env->FindClass("HelloWorldSwing");
if (cls == 0) qDebug()<<"Sorry, I can't find the class";
jmethodID get_main_id =
env->GetStaticMethodID(cls,"main","([Ljava/lang/String;)V");
jstr=env->NewStringUTF("hola que tal");
stringClass=env->FindClass("java/lang/String");
args=env->NewObjectArray(1,stringClass,jstr);
env->CallStaticVoidMethod(cls,get_main_id,args);
jvm->DestroyJavaVM();
qDebug()<<"Java VM destroy\n";
}//end main.
HelloWorldSwing 是一个显示带有“Hello world”文本的 jframe 的类,但不显示任何内容,如果我在 java 类中编写 system.out.println("hello world"),则可以正常运行。