Launching Java programs from Java programs

Status
Not open for further replies.

R2P2

Ars Praefectus
3,783
Subscriptor
I'd like a Java program that I'm working on to be able to run other Java programs. Using Runtime.exec("java ...") seems like the most obvious way to do it, but I can't figure out how to capture the output from the program, or get input to its System.in.

The "best" workaround I've found is to use Reflection to load the Method object for the other program's main(), then use Method.invoke() to run it. The problem is, if the other program ever, say, calls System.exit(), it'll end up exiting *my* program. Likewise, if it changes the Look and Feel, it'll probably change the Look and Feel of my program. So, this is do-able, but not particularly safe.

Would it be possible to use Runtime.exec(), and have something like a JTextArea act as a console? None of my attempts at that have worked; the best I've been able to do is get the output after the program exits, which isn't especially useful for interactive programs.

Has anyone got any other ideas?
 

gumbo

Ars Tribunus Angusticlavius
6,107
IIRC the Process class returned from exec allows you to get the console IO streams. For example, if the console buffer is filled while a program runs, you must capture the input stream and handle it, or your program is not guarenteed to finish to completion.

Edit: Yep. ;)

Code:
Process p = Runtime.exec(ecec string);
InputStream is = p.getInputStream();

Edit 2: JJJ - If you are on a Windows system (and perhaps a *nix system), then redirecting from a file will not catch the System.err output.

gubmo

[This message was edited by gubmo on April 11, 2002 at 19:08.]
 

Pika

Seniorius Lurkius
21
Originally posted by gubmo:
Edit 2: JJJ - If you are on a Windows system (and perhaps a *nix system), then redirecting from a file will not catch the System.err output.

You can redirect output in such a way that you capture System.err at the cmd line:

program 2>error.txt >standard.txt

should do the trick.

Edit: This is on a Windows box, not sure if you can do this on *nix.
 

R2P2

Ars Praefectus
3,783
Subscriptor
Redirecting output to a file doesn't seem particularly useful for interactive programs; I'd have to have one program read from the file while the other writes to it, and I don't know how well that would work. Likewise, I'd probably need to use a file to send input to the program.

I've tried using the I/O streams, but I haven't been able to get anything out of the OutputStream until the process completes. Any suggestions on how to use the streams?
 

Midlands

Ars Praetorian
535
Subscriptor
We've had good luck with Process.getInputStream(), Process.getOutputStream(), etc.; however, as Java has no nonblocking I/O really, we spin up new threads that transcribe from buffers to these streams and vice-versa. This works reasonably well; just make sure you synchronize properly.

Having said that, it is, in general, not terribly reliable to use Java's execution methods. If you're doing something casual, it's probably fine, but beyond that I would in fact suggest writing JNI code. I wrote a remote-execution daemon once using Java, and trying to do process control from pure Java was an exercise in pointlessness.
 
Status
Not open for further replies.