473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 4252
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Bobbitt | last post by:
I've recently decided to switch from compiled Apache/PHP to RPM's of both and I'm having a problem I can't seem to solve. I have a Perl script called php_include.cgi that parses a PHP file for...
5
by: Steve Lutz | last post by:
Hello, I have a page that creates a class, and then on certain conditions, redirects user to another page. The class has a Class_Terminate() function that saves itself to a database. The class...
3
by: amine | last post by:
Hi, I have created a proxy class from a wsdl document and I want to add it to my project. but the generated proxy class contains packages like system.web that are not supported by the .NET...
0
by: Dale McGorman | last post by:
The following is some code that I am trying to bring over from VB 6.0, that I have working there. I am trying to get to where I can talk to a USB device. I am stuck on how to correctly pass params...
2
by: Jason | last post by:
Hello, I have a class, transCore, that does certain work, and by default, prints its progress to the stand output. Later, I will to write a GUI class that encapsulate the transCore class. I...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
2
by: Breeto | last post by:
Can anyone please tell me why the following doesn't work... using System; using System.Web; namespace AspTests {
7
by: pagarwalla1234 | last post by:
Hi, Below is the part of code from a test FW I am working on : sub run { my $self = shift; $self->SUPER::LogDirGen( ); # priti my $config = new Config::Simple( $self->{ config }...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.