2012-11-08 46 views
2

轉換我有一個小的大問題,JNI和C++/QT: 這是我的代碼:JNI找不到類,如果JVM的參數從其他類型的

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 
} 

我已經測試參數和相同。 classpath具有相同的值,但註釋的代碼正確運行,其他代碼找不到類,有什麼想法?

+1

什麼是你的問題?我不清楚你有什麼問題。 – maba

+0

「我測試過的論點是否相同」是什麼意思? 「classpath具有相同的值」是什麼意思?和什麼一樣?什麼是「評論代碼」和「其他代碼」?這個問題到底是什麼? – EJP

回答

0

您的循環中包含一個錯誤:

for(int i; i < arguments.size(); i++) 

應該

for(int i = 0; i < arguments.size(); i++) 

否則i會有任何隨機初始值(至少在我的編譯器)

相關問題