By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

Can a Calling Class Redirect the Called Class's Output?

P: n/a
I have a Java program, composed of a number of classes, that I've been
running as a command line program. It logs everything it does and, as it
logs each event, it also prints that line of info to the console.

I would like to create a wrapper class to open a window, call my
application, and redirect all the output to a TextField in the window. I
know I could have the wrapper simply run a command line instance of the app
(like "java app.class"), but that means the delay and waste of memory by
loading a 2nd JVM.

Is there any way my wrapper class can intercept all the printed output (it
goes to standard output) from the app classes it calls? (I would think
once I intercept it, I can redirect it, right?)

Thanks!

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
The quickest solution that comes to mind is to write an Ouput class
that displays a window with the text area. Something like this:

import javax.swing.* ;
import java.io.* ;

public class Output
{
public static void main(String[] args)
{
JFrame f = new JFrame() ;
JTextArea ta = new JTextArea() ;
JScrollPane scroller = new JScrollPane(ta) ;
f.getContentPane().add(scroller) ;
f.setVisible(true) ;

BufferedReader in =
new BufferedReader(new InputStreamReader (System.in)) ;
String input="" ;
while(true)
{
try{
input = in.readLine() ;
ta.append(input+"\n") ;
}catch(Exception e){System.out.println(e) ;}
}
}
}

Now you can pipe the output of your old program into this one using
the command:
$> java Application | java Output

The best thing is that this will work with any program. You dont need
to change any source code...

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."
Jul 17 '05 #2

P: n/a
Hal Vaughan wrote:
I have a Java program, composed of a number of classes, that I've been
running as a command line program. It logs everything it does and, as it
logs each event, it also prints that line of info to the console.

I would like to create a wrapper class to open a window, call my
application, and redirect all the output to a TextField in the window. I
know I could have the wrapper simply run a command line instance of the app
(like "java app.class"), but that means the delay and waste of memory by
loading a 2nd JVM.

Is there any way my wrapper class can intercept all the printed output (it
goes to standard output) from the app classes it calls? (I would think
once I intercept it, I can redirect it, right?)


Hal,

You can use the System.setOut() method to change the standard output
stream. In your case, you will want to implement a PrintStream that
appends text to the text area.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #3

P: n/a
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<KO********************@twister.nyroc.rr.com> ...
Hal,

You can use the System.setOut() method to change the standard output
stream. In your case, you will want to implement a PrintStream that
appends text to the text area.

HTH,
Ray


Which would mean having to change all the existing System.out.print statements....

--
Fahd Shariff
http://www.fahdshariff.cjb.net/
"Let the code do the talking..."
Jul 17 '05 #4

P: n/a
Thanks to those who responded.

Since my programming skills are self-taught (other than a class in Assembler
over a decade ago) is that I often am not sure where to even start looking
for a solution. In the case of Java, it can be hard to know just where in
the API to look for something.

Both answers gave me enough info (and examples) that I was able to make it
work, and also look up more info so I understood what was going on.

Thanks!

Hal

Hal Vaughan wrote:
I have a Java program, composed of a number of classes, that I've been
running as a command line program. It logs everything it does and, as it
logs each event, it also prints that line of info to the console.

I would like to create a wrapper class to open a window, call my
application, and redirect all the output to a TextField in the window. I
know I could have the wrapper simply run a command line instance of the
app (like "java app.class"), but that means the delay and waste of memory
by loading a 2nd JVM.

Is there any way my wrapper class can intercept all the printed output (it
goes to standard output) from the app classes it calls? (I would think
once I intercept it, I can redirect it, right?)

Thanks!

Hal


Jul 17 '05 #5

P: n/a
Fahd Shariff wrote:
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<KO********************@twister.nyroc.rr.com> ...
Hal,

You can use the System.setOut() method to change the standard output
stream. In your case, you will want to implement a PrintStream that
appends text to the text area.

HTH,
Ray

Which would mean having to change all the existing System.out.print statements....


Fahd,

Why do you say that? The documentation says that System.setOut() will
change the standard output stream, i.e., the stream underlying
System.out. I took this to mean it the equivalent of doing something like

System.out = myPrintStream;

(which you can't do because System.out is final).

I was going to say that I haven't tried this technique, but instead I
decided to give it a try and indeed, it worked:

----- Start SystemOut.java -------------
public class SystemOut
{
public static void main(String args[])
{
System.out.println("hello");
}
}
----- End SystemOut.java -------------
----- Start Redirect.java -------------
import java.io.*;

public class Redirect
{
public static void main(String args[]) throws IOException
{
PrintStream out = null;
try
{
out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(args[0])));
System.setOut(out);
SystemOut.main(null);
}
finally
{
out.flush();
out.close();
}
}
}
----- End Redirect.java -------------
$ java -cp . Redirect hello.txt
$ cat hello.txt
hello
$ javac Redirect.java SystemOut.java
$ java -cp . SystemOut
hello
$ java -cp . Redirect hello.txt
$ cat hello.txt
hello
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.