Redirecting System.out.println()  
Author Message
Ike





PostPosted: 2006-2-5 19:27:00 Top

java-programmer, Redirecting System.out.println() Is anyone familiar with a way to redirect System.out.println()? I'd like to
redirect it to a JTextArea if possible. Thanks, Ike


 
Roedy Green





PostPosted: 2006-2-5 20:38:00 Top

java-programmer >> Redirecting System.out.println() On Sun, 05 Feb 2006 11:26:41 GMT, "Ike" <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

>Is anyone familiar with a way to redirect System.out.println()? I'd like to
>redirect it to a JTextArea if possible. Thanks, Ike

I think it is called System.setOut IIRC. But it wont let you direct to
a JTextArea. It does not implement any sort of stream interface.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Daniel Dyer





PostPosted: 2006-2-5 20:44:00 Top

java-programmer >> Redirecting System.out.println() On Sun, 05 Feb 2006 12:37:34 -0000, Roedy Green
<email***@***.com> wrote:

> On Sun, 05 Feb 2006 11:26:41 GMT, "Ike" <email***@***.com> wrote,
> quoted or indirectly quoted someone who said :
>
>> Is anyone familiar with a way to redirect System.out.println()? I'd
>> like to
>> redirect it to a JTextArea if possible. Thanks, Ike
>
> I think it is called System.setOut IIRC. But it wont let you direct to
> a JTextArea. It does not implement any sort of stream interface.

But there is, of course, nothing to stop you from writing an OutputStream
implementation that appends to a JTextArea.

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk
 
 
Ian Mills





PostPosted: 2006-2-5 21:32:00 Top

java-programmer >> Redirecting System.out.println() Ike wrote:

> Is anyone familiar with a way to redirect System.out.println()? I'd like to
> redirect it to a JTextArea if possible. Thanks, Ike
>
>
Why exactly do want to do this? Surly it would make more sense to send
your output directly to the JTextArea rather than System.out
 
 
Frank D. Greco





PostPosted: 2006-2-6 2:30:00 Top

java-programmer >> Redirecting System.out.println() Ian Mills <email***@***.com> sez:

>Ike wrote:
>
>> Is anyone familiar with a way to redirect System.out.println()? I'd like to
>> redirect it to a JTextArea if possible. Thanks, Ike
>>
>>
>Why exactly do want to do this? Surly it would make more sense to send
>your output directly to the JTextArea rather than System.out

Perhaps Ike wanted to capture the output of an exec()-ed pgm?

Frank G.
 
 
Frank D. Greco





PostPosted: 2006-2-6 2:36:00 Top

java-programmer >> Redirecting System.out.println() Roedy Green <email***@***.com> sez:

>On Sun, 05 Feb 2006 11:26:41 GMT, "Ike" <email***@***.com> wrote,
>quoted or indirectly quoted someone who said :
>
>>Is anyone familiar with a way to redirect System.out.println()? I'd like to
>>redirect it to a JTextArea if possible. Thanks, Ike
>
>I think it is called System.setOut IIRC. But it wont let you direct to
>a JTextArea. It does not implement any sort of stream interface.

I asked the Swing team for this (and integration with NIO) a *long* time ago.
No response. [I got the same response when I asked for a skinnable PLAF back in
1999]

It'd be pretty easy to write QnD wrappers for monitoring file logs for
enterprise app developers who aren't wizards.

Frank G.
+==========================================+
| Crossroads Technologies Inc. |
| www.CrossroadsTech dot com |
| fgreco at REMOVE!cross!roads!tech!dot com|
+==========================================+
 
 
Ike





PostPosted: 2006-2-6 3:08:00 Top

java-programmer >> Redirecting System.out.println() Just looking to capture what is output to the plugin console without having
to try to talk people through how to get it out of there! -Ike

"Frank D. Greco" <email***@***.com> wrote in
message news:email***@***.com...
> Ian Mills <email***@***.com> sez:
>
> >Ike wrote:
> >
> >> Is anyone familiar with a way to redirect System.out.println()? I'd
like to
> >> redirect it to a JTextArea if possible. Thanks, Ike
> >>
> >>
> >Why exactly do want to do this? Surly it would make more sense to send
> >your output directly to the JTextArea rather than System.out
>
> Perhaps Ike wanted to capture the output of an exec()-ed pgm?
>
> Frank G.


 
 
steve





PostPosted: 2006-2-6 5:54:00 Top

java-programmer >> Redirecting System.out.println() On Mon, 6 Feb 2006 03:07:45 +0800, Ike wrote
(in article <52sFf.11086$email***@***.com>):

> Just looking to capture what is output to the plugin console without having
> to try to talk people through how to get it out of there! -Ike
>
> "Frank D. Greco" <email***@***.com> wrote in
> message news:email***@***.com...
>> Ian Mills <email***@***.com> sez:
>>
>>> Ike wrote:
>>>
>>>> Is anyone familiar with a way to redirect System.out.println()? I'd
> like to
>>>> redirect it to a JTextArea if possible. Thanks, Ike
>>>>
>>>>
>>> Why exactly do want to do this? Surly it would make more sense to send
>>> your output directly to the JTextArea rather than System.out
>>
>> Perhaps Ike wanted to capture the output of an exec()-ed pgm?
>>
>> Frank G.
>
>

you need to write a wrapper. something like this.
yes i know this code is a piece of crap, and a lot of it is redundant(i
spent some time getting it to work), but it works.





package com.Errors;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

import java.util.*;
import java.util.regex.*;


//this should only be used with the log4j package
//it is to trap any errors that might be directed to the console screen.
public class consoleIntercept extends PrintStream {
private PrintStream console = System.out;
private PrintStream err = System.err;


public consoleIntercept() {
super(System.out, true); // Autoflush
System.setOut(this);
System.setErr(this);




}


//error_stuff is the package that log4j's the shit sent to it.

// public PrintStream getConsole() { return console; }
public void dispose() {
System.setOut(console);
System.setErr(err);


}

// Override all possible print/println methods to send
public void print(String x) {
console.print(x); // keep it to the console as well
Error_stuff.handleError(x);
}

public void println(String x) {
console.println(x); // keep it to the console as well
Error_stuff.handleError(x);
}
public void print(Object x) {
Error_stuff.handleError(x.toString());

}
public void println(Object x) {

Error_stuff.handleError(x.toString());
}
/*
public void print(boolean x) {
console.print(x);

}
public void println(boolean x) {
console.println(x);
}
public void print(char x) {
console.print(x);

}
public void println(char x) {

console.println(x);
fout.println(x);
}
public void print(int x) {
console.print(x);

}
public void println(int x) {

console.println(x);
fout.println(x);
}
public void print(long x) {
console.print(x);

}
public void println(long x) {

console.println(x);
fout.println(x);
}
public void print(float x) {
console.print(x);

}
public void println(float x) {

console.println(x);
fout.println(x);
}
public void print(double x) {
console.print(x);

}
public void println(double x) {

console.println(x);
fout.println(x);
}
public void print(char[] x) {
console.print(x);

}
public void println(char[] x) {

console.println(x);
fout.println(x);
}


public void println() {
if(false) console.print("println");

console.println();
fout.println();
}
public void write(int x) {
Error_stuff.handleError(x.toString());
}

public void
write(byte[] buffer, int offset, int length) {
console.write(buffer, offset, length);

}
public void write(int b) {
console.write(b);
fout.write(b);
}
*/
}

 
 
Thomas Fritsch





PostPosted: 2006-2-6 8:41:00 Top

java-programmer >> Redirecting System.out.println() "Daniel Dyer" <email***@***.com> wrote:
> On Sun, 05 Feb 2006 12:37:34 -0000, Roedy Green wrote:
>> On Sun, 05 Feb 2006 11:26:41 GMT, "Ike" <email***@***.com> wrote:
>>
>>> Is anyone familiar with a way to redirect System.out.println()? I'd
>>> like to
>>> redirect it to a JTextArea if possible. Thanks, Ike
>>
>> I think it is called System.setOut IIRC. But it wont let you direct to
>> a JTextArea. It does not implement any sort of stream interface.
>
> But there is, of course, nothing to stop you from writing an OutputStream
> implementation that appends to a JTextArea.
>
I guess this wheel has already been invented several times.
When people implement such thing, they tend to call their class
"TextAreaOutputStream".
Therefore you should search the "comp.lang.java.*" groups for
"TextAreaOutputStream".
See http://groups.google.com

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')


 
 
Ranganath Kini





PostPosted: 2006-2-6 13:59:00 Top

java-programmer >> Redirecting System.out.println() Ike wrote:
> Is anyone familiar with a way to redirect System.out.println()? I'd like to
> redirect it to a JTextArea if possible. Thanks, Ike

Dear Ike,

I have written a small class which can be used to redirect output to a
JTextArea control. Here is the code:

/*
* @(#) TextAreaOutputStream.java
*
*/

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;

/**
* Creates a new instance of TextAreaOutputStream which writes
* to the specified instance of javax.swing.JTextArea control.
*
* @param control A reference to the javax.swing.JTextArea
* control to which the output must be redirected
* to.
*/
public TextAreaOutputStream( JTextArea control ) {
textControl = control;
}

/**
* Writes the specified byte as a character to the
* javax.swing.JTextArea.
*
* @param b The byte to be written as character to the
* JTextArea.
*/
public void write( int b ) throws IOException {
// append the data as character to the JTextArea control
textControl.append( String.valueOf( ( char )b ) );
}
}

To use the TextAreaOutputStream, use the following code:

JTextArea txtConsole = new JTextArea();
PrintStream out = new PrintStream(
new TextAreaOutputStream( txtConsole
) );
System.setOut( out );
System.setErr( out );
System.out.println( "Hello World!" );

Here is an output screenshot of a program I created with the
TextAreaOutputControl:

http://www.geocities.com/buddybob23/ConsoleIntercept.jpg

Hope it helps!

Best Regards,
Ranganath Kini