Launching Java programs from Java programs

Status
You're currently viewing only R2P2's posts. Click here to go back to viewing the entire thread.
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?
 

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?
 
Status
You're currently viewing only R2P2's posts. Click here to go back to viewing the entire thread.
Not open for further replies.