Search code examples
javamultithreadingloggingapache-commons-exec

Getting all output data form console when running process with Apache Commons Exec


The thing is... I'm running a process with the DefaultExecutor class of org.apache.commons.exec libraries. Like this:

public class Main {

public static void main(String[] args) throws IOException, InterruptedException {

    CommandLine cmd = new CommandLine("java");
    DefaultExecutor exec = new DefaultExecutor();
    exec.setExitValue(1);
    exec.execute(cmd);
}

I need to take that output "on the run" with another thread, to log it elsewhere. What is the best way of accomplish that?


Solution

  • Use a PipedOutputStream and a PipedInputStream. You can find an example here. Don't forget to close your streams.