Solved

Run java program on separate VM and see the output

Posted on 2002-05-31
7
218 Views
Last Modified: 2010-03-31
Hi!
I want to run from my java program another java program.
I want to run it on a different VM and to see its output.
I've succeeded running it using Runtime.getRuntime().exec() command, but I can't see the output.
What can I do?
0
Comment
Question by:rzvika2
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7046741
Use the input stream of the process:

Process p = Runtime.getRuntime().exec();
java.io.InputStream in = p.getInputStream();

int r;
while ((r=in.read()) != -1) System.out.write(r);
0
 

Author Comment

by:rzvika2
ID: 7046838
Thanks!

And if I want it to print it on a seperate window thatn the one that I'm running?
Or to put it into a JTextArea?
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7046850
you can wrap the input stram with a reader and append the read content to the JTextArea content:


javax.swing.JTextArea textArea;

Process p = Runtime.getRuntime().exec();
java.io.InputStream in = p.getInputStream();
java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream

String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
    textArea.append(str + "\r\n");
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rzvika2
ID: 7055490
Hi again!
And last thing, how can I redirect the output of my java program to a JTextArea (I don't have the Process object)?
0
 

Expert Comment

by:pouli
ID: 7065933
This is even easier

The only thing you ahve to do is to reset the default System.out or System.err strems to the ones you want with the method

System.setOut();
System.setErr();

See the API for the System class to see what I mean.

0
 

Author Comment

by:rzvika2
ID: 7066169
thanks pouli!
...but how can I connect between this and the JTextArea?
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 7066269
public class BuffOutput extends java.io.FilterOutputStream {
javax.swing.JTextArea textArea;
StringBuffer output;

public BuffOutput(java.io.OutputStream out, javax.swing.JTextArea ta) {
  super(out);
  textArea = ta;
  output = StringBuffer();
}

public void write(int i) throws java.io.IOException {
  output.append((char) i);
  textArea.setText(output.toString());
  // if you want - you can just append - instead of setting the whole text
}
}

to use this thread in the initialization of your program just call:

OutputStream out = new BuffOutput(System.out, yourTextArea);
System.setOut(out);
System.setErr(out);

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question