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]
# 1
System.setOut(yourPrintStream);//and/orSystem.setErr(yourPrintStream);
Hippolytea at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 2

> 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

kajbja at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 3
Code monkey type fast!
Hippolytea at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 4
Sorry just got back to work. Let me try out things you suggested.
kurmata at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 5

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 ?

kurmata at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 6
You'll know it because read methods on the stream will successfully return some data which was written by that API.
Dalzhima at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 7

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 ?

kurmata at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 8
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?
Hippolytea at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 9

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

kurmata at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 10
So know you know what you must do?
Hippolytea at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 11
> 1. You defined your own PrintStream subclass? -- NoDo that then. Kaj
kajbja at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 12
> > 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.
Hippolytea at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 13
Can I use the existing PrintStream class instead of subclassing it ?
kurmata at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 14
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);
kurmata at 2007-7-12 9:45:35 > top of Java-index,Java Essentials,New To Java...
# 15
> Can I use the existing PrintStream class instead of subclassing it ?No.
Hippolytea at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 16
If I subclass the PrintStream which methods should be overridden ?
kurmata at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 17
> If I subclass the PrintStream which methods should be overridden ?I would override them all, just in case.
Hippolytea at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 18
so when the third party application does a System.out.println(......) , Which method of the new PrintStream subclass is really called ?
kurmata at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 19
Probably the corresponding println method in your PrintStream class.
Hippolytea at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 20
I will try and get back with you folks. Thanks
kurmata at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 21
> 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 > top of Java-index,Java Essentials,New To Java...
# 22
I think it works now. I did not complete it . It seems that the println method is getting called.Thank you folks
kurmata at 2007-7-21 21:09:41 > top of Java-index,Java Essentials,New To Java...