I am attempting to make a Java program in which a user can select any .class
or .jar
file from their computer. My program will then pop up a JInternalFrame
with a JEditorPane
in it as the console, capturing any console output from the user's program. Note that I do not want to capture just System.err or System.out calls, but ALL PrintStream calls that go to the console.
(individual question from IDE-Style program running )
If you're starting the user's .jar file using Runtime.exec(), you'll get a Process object. That Object will allow you access to the launched processes System.out, System.in and System.err streams.
See: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html
You can take read from the err and out streams, and append to your JEditorPane using the usual setText type methods.
You can catch everything that is printed through
System.out
usingSystem.setOut
like this:Prints:
(There is a
System.setErr
andSystem.setIn
that works similarly.)If you want to catch stuff that the "subprogram" prints through
System.out.println
you're in trouble, becauseSystem.out
is a static so if you launch multiple "subprograms" you'll end up with a mess (since you can't hand a separateSystem
class to each subprogram).In a situation like this, I honestly think it would be a better idea to launch a separate process through ProcessBuilder. The standard input / output streams of the resulting process could easily be logged.
(p.s. When I think about it, you could probably check the current thread group in the
println
implementation, and from that decide which subprogram that actually invoked theprintln
method)