Solved

Run java program on separate VM and see the output

Posted on 2002-05-31
7
221 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
Industry Leaders: 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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