使用 Apache Commons Exec 运行进程时从控制台获取所有输出数据
问题是......我正在使用 org.apache.commons.exec
库的 DefaultExecutor
类运行一个进程。像这样:
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);
}
我需要使用另一个线程“运行时”获取该输出,以将其记录在其他地方。实现这一目标的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
PipedOutputStream
和PipedInputStream
。您可以在此处找到示例。不要忘记关闭
你的直播。Use a
PipedOutputStream
and aPipedInputStream
. You can find an example here. Don't forget toclose
your streams.您可能应该看看
log4j
,这是 Apache 的一个相当有用的项目。在我最近从事的一个项目中,log4j
用于将来自各个线程的所有日志放入一个方便的文件中。只需确保您以只有一个实例可用的方式构建记录器,这应该可以解决您的问题。不幸的是,我只是一名实习生,团队设置日志系统时我并不在场,所以我实际上无法帮助您进行配置。幸运的是,该项目的网站似乎有大量文档可以帮助您。
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.