Solved

Run java program on separate VM and see the output

Posted on 2002-05-31
7
215 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now