调用 Runtime.exec 时捕获标准输出
当客户端计算机上遇到网络问题时,我希望能够运行一些命令行并将结果通过电子邮件发送给自己。
我发现 Runtime.exec 将允许我执行任意命令,但将结果收集到字符串中更有趣。
我意识到我可以将输出重定向到文件,然后从文件中读取,但我的敏锐感觉告诉我有一种更优雅的方法可以做到这一点。
建议?
When experiencing networking problems on client machines, I'd like to be able to run a few command lines and email the results of them to myself.
I've found Runtime.exec will allow me to execute arbitrary commands, but Collecting the results in a String is more interesting.
I realize I could redirect output to a file, and then read from the file, but my spidey sense is telling me there's a more elegant way of doing it.
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要捕获过程中的 std out 和 std err。 然后您可以将 std 写入文件/邮件或类似文件。
有关详细信息,请参阅本文 ,特别要注意在单独线程中捕获 stdout/err 的 StreamGobbler 机制。 这对于防止阻塞至关重要,如果操作不当,则会导致许多错误!
You need to capture both the std out and std err in the process. You can then write std out to a file/mail or similar.
See this article for more info, and in particular note the
StreamGobbler
mechanism that captures stdout/err in separate threads. This is essential to prevent blocking and is the source of numerous errors if you don't do it properly!使用 ProcessBuilder。 调用 start() 后,您将得到一个 Process 对象,您可以从中获取 stderr 和 stdout 流。
更新:ProcessBuilder 为您提供更多控制; 您不必使用它,但从长远来看,我发现它更容易。 特别是能够将 stderr 重定向到 stdout,这意味着您只需吸收一个流。
Use ProcessBuilder. After calling start() you'll get a Process object from which you can get the stderr and stdout streams.
UPDATE: ProcessBuilder gives you more control; You don't have to use it but I find it easier in the long run. Especially the ability to redirect stderr to stdout which means you only have to suck down one stream.
对于不会产生太多输出的进程,我认为这个简单的解决方案利用 Apache IOUtils 就足够了:
警告:但是,如果您的进程生成大量输出,则此方法可能会导致问题,如 流程类 JavaDoc:
For processes that don't generate much output, I think this simple solution that utilizes Apache IOUtils is sufficient:
Caveat: However, if your process generates a lot of output, this approach may cause problems, as mentioned in the Process class JavaDoc:
使用Plexus Utils,Maven使用它来执行所有外部进程。
Use Plexus Utils, it is used by Maven to execut all external processes.
这是我多年来一直使用的助手类。 一小班。 它有 JavaWorld Streamgobbler 类来修复 JVM 资源泄漏。 不知道对于 JVM6 和 JVM7 是否仍然有效,但不会造成伤害。 助手可以读取输出缓冲区以供以后使用。
以下是从 .vbs 脚本读取输出的示例,但 Linux sh 脚本的工作类似。
This is my helper class been using for years. One small class. It has JavaWorld streamgobbler class to fix JVM resource leaks. Don't know if still valid for JVM6 and JVM7 but does not hurt. Helper can read output buffer for later use.
Here is an example reading output from .vbs script but similar works for linux sh scripts.
VerboseProcess
< 可以帮助您:jcabi-log 中的 /a> 实用程序类 你需要的依赖:
VerboseProcess
utility class from jcabi-log can help you:The only dependency you need:
使用 Runtime.exec 为您提供一个进程。 您可以使用 getInputStream< /a> 获取此进程的标准输出,并将此输入流放入 String 中,例如通过 StringBuffer 。
Using Runtime.exec gives you a process. You can these use getInputStream to get the stdout of this process, and put this input stream into a String, through a StringBuffer for example.
Runtime.exec() 返回一个 Process 对象,您可以从中提取您运行的任何命令的输出。
Runtime.exec() returns a Process object, from which you can extract the output of whatever command you ran.