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

S

sp999

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top