Java Runtime.getRuntime(): getting output from executing a command line program

前端 未结 12 1359
清歌不尽
清歌不尽 2020-11-22 00:18

I\'m using the runtime to run command prompt commands from my Java program. However, I\'m not aware of how I can get the output the command returns.

Here is my code:

12条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:53

    If use are already have Apache commons-io available on the classpath, you may use:

    Process p = new ProcessBuilder("cat", "/etc/something").start();
    String stderr = IOUtils.toString(p.getErrorStream(), Charset.defaultCharset());
    String stdout = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());
    

提交回复
热议问题