Run java program on separate VM and see the output

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?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Venci75Connect With a Mentor Commented:
public class BuffOutput extends {
javax.swing.JTextArea textArea;
StringBuffer output;

public BuffOutput( out, javax.swing.JTextArea ta) {
  textArea = ta;
  output = StringBuffer();

public void write(int i) throws {
  output.append((char) i);
  // 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);

Use the input stream of the process:

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

int r;
while (( != -1) System.out.write(r);
rzvika2Author Commented:

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?
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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(); in = p.getInputStream(); reader = new; // wrapping the stream

String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
    textArea.append(str + "\r\n");
rzvika2Author Commented:
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)?
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


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

rzvika2Author Commented:
thanks pouli!
...but how can I connect between this and the JTextArea?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.