4 回答
TA贡献1801条经验 获得超16个赞
我最近遇到了这个问题,只想提及自Java 7以来,流程构建器api已得到扩展。现在可以使用以下方法解决此问题:
ProcessBuilder pb = new ProcessBuilder("yourcommand");
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();
TA贡献1794条经验 获得超7个赞
我遇到了类似的问题,并且正在使用以下代码。
Process p = Runtime.getRuntime().exec(".....");
p.waitFor();
String line;
BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while((line = error.readLine()) != null){
System.out.println(line);
}
error.close();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line=input.readLine()) != null){
System.out.println(line);
}
input.close();
OutputStream outputStream = p.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.println();
printStream.flush();
printStream.close();
添加回答
举报