Java Process Builder总是在错误流中提供数据

时间:2018-12-08 04:33:35

标签: java stream processbuilder

我有以下代码:

ProcessBuilder pb = new ProcessBuilder(CMD_ARRAY);
process = pb.start();
FrameworkLogger.log("Waiting for 120 seconds");
boolean code = process.waitFor(120, TimeUnit.SECONDS);
System.out.println("code is "+code);
output = getStream(process.getInputStream());
System.out.println("getInputStream:"+output);
error = getStream(process.getErrorStream());
System.out.println("getErrorStream:"+error);


public String getStream(InputStream inputStream) throws Exception {
    StringBuilder sb = new StringBuilder();
    BufferedReader br1 = null;
    String line;
    try {
         br1 = new BufferedReader(new InputStreamReader(inputStream));
        line = null;
        while ((line = br1.readLine()) != null) {
            sb.append(line + System.getProperty("line.separator"));
        }
    } finally {
        br1.close();
    }
    return sb.toString();
}

我总是使代码为true,但是来自getInputStream()的流为空,并且所需的流位于getErrorStream()中。为什么会这样呢?如何在getInputStream()中获得所需的流?

0 个答案:

没有答案