DaeYoung DaeYoung - 2 months ago 7
Java Question

How does System.out direct output to console in eclipse?

While looking at somebody's maven Java project in eclipse, I noticed that one of saved run configurations has following maven command to run:

test -Dtest=fooTest -Dcom.awesomesite=System.out


When I inspected fooTest.java file, I noticed following statements:

public final static String BAR = "com.awesomesite";
....
this.whereToWrite = System.getProperty(BAR);


Obviously, an instance variable "whereToWrite" represents where the output data should go to. Since I am beginner to Java, I just want to understand how does System.out direct output to be printed on console.

Answer

When you launch a run configuration Eclipse starts a new process to run it using Runtime.getRuntime().exec(...). This returns a Process object representing the new process.

Process has a getInputStream() method which returns an InputStream connected to the standard output of the process (which is where System.out normally writes). Eclipse reads from this input stream and outputs what it receives on the console.

Eclipse also reads from the process error stream using getErrorStream().