JNI:Can anybady help me:Howto:Enabling Messages Printed Using System.out.printIn

From: sp999 (zlf_at_sunyard.com)
Date: 01/13/04

  • Next message: sp999: "f"
    Date: 12 Jan 2004 18:31:39 -0800
    
    

    I hoped to use a dynamic-created console to display the messages from
    my java program running in JVM which had been created by
    JNI_CreateJavaVM.But it seems useless. My code is like this:
    void CTcDlg::OnButton1()
    {
            //
            int hCrt;
            FILE* hf;
            AllocConsole();//dynamic create a consolem for the GUI program
            hCrt = _open_osfhandle(
                            (long) GetStdHandle(STD_ERROR_HANDLE),
                            _O_TEXT
                            );
            hf = _fdopen( hCrt, "w" );
            *stdout = *hf;
            int i = setvbuf( stdout, NULL, _IONBF, 0 );
            printf("OK\n");//it works fine!!!!!

            {//begin jvm invoke
                    JavaVM* jvm=NULL;
                    JavaVMOption jvmopitions[1];
                    JavaVMInitArgs vm_args;
                    JNIEnv* env;
                    
                    // jvmopitions[0].optionString="-verbose:jni";
                    jvmopitions[0].optionString="-Djava.class.path=E:\\programs\\tc";
                    
                    vm_args.version = JNI_VERSION_1_2;
                    vm_args.nOptions = 1;
                    vm_args.options = jvmopitions;
                    
                    int status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
                    
                    if(status==JNI_ERR)
                    {
                            MessageBox("ErrorCreate JVm");
                    }
                    
                    jclass cls,stringCls;
                    jobject jstrArray;
                    jmethodID mid;
                    try{
                            
                    stringCls=env->FindClass("java/lang/String");
                    jstrArray=env->NewObjectArray(1,stringCls,NULL);
                    cls=env->FindClass("PrintABC");
                    mid=env->GetStaticMethodID(cls,"main","([Ljava/lang/String;)V");
                    env->CallStaticVoidMethod(cls,mid,jstrArray);
                    }
                    catch(...)
                    {
                    }
                    
    // if(env->ExceptionOccurred())
    // env->ExceptionDescribe();//it didn`t work

                    if(env->ExceptionOccurred())
                    {
                            MessageBox("Error in try!");
                    }
                    else
                            MessageBox("Succeed during test!");
            }//end of jvm invoke

            FreeConsole();//free console.
    }

    and my java program is like
    class PrintABC{
       public static void main(String argc[]){
         System.out.println("Print ABC from Java.");
       }
    }
    please help me how can I correctly redirect STDOUT ,STDERR.
    and please help me Where can I find some information to deal the
    problem.
    thank you very much!


  • Next message: sp999: "f"