我在错误流而不是输入流
中输出以下命令Runtime rt = Runtime.getRuntime();
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd.exe","/c","java -version"});
Process pr = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line=input.readLine();
System.out.println(line);
请解释
答案 0 :(得分:5)
显然,Java将版本字符串发送到stderr
。这不是一种非常罕见的做法,因为它不是客户端代码的输出。不要被名称ErrorStream
误导:它不仅仅用于错误;它基本上是主要信道旁边的信令侧信道,即stdout
。
答案 1 :(得分:0)
It works file in jdk7
public static void main(String[] args) throws Exception {
Runtime rt = Runtime.getRuntime();
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd.exe","/c","java -version"});
Process pr = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line=input.readLine();
System.out.println(line);
}