“java -version”命令导致错误流

时间:2013-02-27 13:23:00

标签: java cmd

我在错误流而不是输入流

中输出以下命令
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);

请解释

2 个答案:

答案 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);

    }