Event Listner for the console
I dont know whether this is the right forum for this question. May be this question is stupid. I am using a third part y jar file . I have a small swing application which calls this third party utility with certain arguments. This vendor utility spits out System.out.println statements to the console indicating the progress. I want to use this console print out and then update a progress bar for the swing applicatiion. Is there any way to write a console event listener for this?
Something gets printed on the console while the java program is running. could this event be trapped?
Thanks
[611 byte] By [
kurmata] at [2007-11-27 4:35:31]

System.setOut(yourPrintStream);//and/orSystem.setErr(yourPrintStream);
> I dont know whether this is the right forum for this
> question. May be this question is stupid. I am using
> a third part y jar file . I have a small swing
> application which calls this third party utility
> with certain arguments. This vendor utility spits out
> System.out.println statements to the console
> indicating the progress. I want to use this console
> print out and then update a progress bar for the
> swing applicatiion. Is there any way to write a
> console event listener for this?
> Something gets printed on the console while the java
> program is running. could this event be trapped?
>
> Thanks
You can redirect stdout to a stream that you has created. Read that stream and update the progress bar.
Kaj
Sorry just got back to work. Let me try out things you suggested.
I created a PrintStream and did the System.setOut(); How do I know when something is written to this printStream.
This is how my logic works now
I finish my side of the process
Update the progress bar as required
System.setOut( printStream);
Call the third party utility thirdparyProgram.process ( args1.............)
Occassiionally it prints out some message indicating progress
Third party utility completes the process
How do I know about the event my printstream got written ?
You'll know it because read methods on the stream will successfully return some data which was written by that API.
The method I call from the third party utility is a synchronous process. I dont get the controll till the third party process is over.
For example
obj. doSomeThing();
obj.doSomethingWithThirdPartyUtility();
obj.igetControllBack();
My understanding is without explicitly creating a thread or somekind of listener, I dont have access to the PrintStream untill the third party method returns back ?
1. You defined your own PrintStream subclass? 2. You redirect output to it?3. Does this third party code write to System.out/err?4. Does this call methods in your own PrintStream subclass?5. Do you react to these calls appropriately?
1. You defined your own PrintStream subclass? -- No
2. You redirect output to it? -- Yes
3. Does this third party code write to System.out/err? System.out
4. Does this call methods in your own PrintStream subclass? No
5. Do you react to these calls appropriately? I an not calling any printstream methods. All the interaction third party utility have is writing to the printstream object I created
So know you know what you must do?
> 1. You defined your own PrintStream subclass? -- NoDo that then. Kaj
> > 1. You defined your own PrintStream subclass? -- No> > Do that then. It is hard to tell if that is what he is doing now, or he is just being dense.
Can I use the existing PrintStream class instead of subclassing it ?
This is what I did File f = new File("mylogfile.txt");PrintStream printStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)), true);System.setErr(printStream);System.setOut(printStream);
> Can I use the existing PrintStream class instead of subclassing it ?No.
If I subclass the PrintStream which methods should be overridden ?
> If I subclass the PrintStream which methods should be overridden ?I would override them all, just in case.
so when the third party application does a System.out.println(......) , Which method of the new PrintStream subclass is really called ?
Probably the corresponding println method in your PrintStream class.
I will try and get back with you folks. Thanks
> I will try and get back with you folks. ThanksAre you using an IDE? Eclipse can override all methods for you. It's just a few clicks away.Kaj
kajbja at 2007-7-21 21:09:41 >

I think it works now. I did not complete it . It seems that the println method is getting called.Thank you folks