0

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 )

Community
  • 1
  • 1
Ky -
  • 30,724
  • 51
  • 192
  • 308

2 Answers2

3

You can catch everything that is printed through System.out using System.setOut like this:

import java.io.*;

class SystemOutLogging {

    public static void main(String[] args) throws IOException,
                                                  ClassNotFoundException {
        final PrintStream original = System.out;

        System.setOut(new PrintStream("programlog.txt") {
            public void println(String str) {
                process(str + "\n");
            }

            public void print(String str) {
                process(str);
            }

            private void process(String str) {
                // Fill some JEditorPane
                original.println("Program printed: \"" + str + "\"");
            }
        });

        System.out.print("Hello ");
        System.out.println(" World");
    }
}

Prints:

Program printed: "Hello "
Program printed: " World
"

(There is a System.setErr and System.setIn that works similarly.)

If you want to catch stuff that the "subprogram" prints through System.out.println you're in trouble, because System.out is a static so if you launch multiple "subprograms" you'll end up with a mess (since you can't hand a separate System 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 the println method)

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • IDEs like NetBeans seem to have no trouble capturing multiple programs' System.out calls. Also, this does not address the part about placing all the calls in a JEditorPane – Ky - Oct 29 '10 at 19:56
  • NetBeans and Eclipse are extremely complex pieces of software. Don't expect to be able to replicate their systems. The example I provided gives you a "process"-method. You do whatever you need with the strings in that method. Appending it to a JEditorPane shouldn't be too hard from that point. – aioobe Oct 29 '10 at 19:59
  • while it's true that a single programmer should baulk before attempting something the size of Eclipse or NetBeans, they're still just programs. Replicating a bit of their functionality (in this case capturing process output) is not necessarily hard or something outside reasonable expectations. – Burleigh Bear Oct 29 '10 at 21:21
  • @Burleigh Bear, agreed. In this case however, he doesn't want to spawn a new process. I for one can't see how to solve this elegantly... – aioobe Oct 29 '10 at 21:25
  • ah, I didn't read his other question. Silly me. By the way, I like your p.s. in the answer. +1. – Burleigh Bear Oct 29 '10 at 21:37
0

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.

Burleigh Bear
  • 3,274
  • 22
  • 32