Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Run java program on separate VM and see the output

Posted on 2002-05-31
7
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 600 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …
Suggested Courses

609 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