Solved

Run java program on separate VM and see the output

Posted on 2002-05-31
7
220 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 74
ejb wildfly example 2 106
how to debug htl and js pages 8 77
Which is best Image Resizing Web service 11 36
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
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

739 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