开发者

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

开发者 https://www.devze.com 2023-04-05 16:08 出处:网络
The thing is... I\'m running a process with the DefaultExecutor class of org.apache.commons.exec libraries. Like开发者_运维问答 this:

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?


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


You should probably look at log4j, a rather useful project from Apache. In a project I was recently working on, log4j was used to put all of the logs from various threads into one convenient file. Just make sure that you construct the logger in such as way that only one instance of it is available, and this should solve your problem.

Unfortunately, I was only an intern, and was not present when the team set up the logging system, so I can't actually help you with configuration. Luckily the project's website appears to have plenty of documentation to help you out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号