Capturing stdout when calling Runtime.exec

2018-12-31 03:03发布

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?

8条回答
琉璃瓶的回忆
2楼-- · 2018-12-31 03:21

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.

查看更多
春风洒进眼中
3楼-- · 2018-12-31 03:28

VerboseProcess utility class from jcabi-log can help you:

String output = new VerboseProcess(
  new ProcessBuilder("executable with output")
).stdout();

The only dependency you need:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-log</artifactId>
  <version>0.7.5</version>
</dependency>
查看更多
看风景的人
4楼-- · 2018-12-31 03:30

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.

查看更多
梦寄多情
5楼-- · 2018-12-31 03:34

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!

查看更多
荒废的爱情
6楼-- · 2018-12-31 03:38

For processes that don't generate much output, I think this simple solution that utilizes Apache IOUtils is sufficient:

Process p = Runtime.getRuntime().exec("script");
p.waitFor();
String output = IOUtils.toString(p.getInputStream());
String errorOutput = IOUtils.toString(p.getErrorStream());

Caveat: However, if your process generates a lot of output, this approach may cause problems, as mentioned in the Process class JavaDoc:

The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

查看更多
君临天下
7楼-- · 2018-12-31 03:38

Runtime.exec() returns a Process object, from which you can extract the output of whatever command you ran.

查看更多
登录 后发表回答