java调用cmd命令并捕获执行结果字符串的代码

 public static void main(String[] args) {
        try {
            Process pro = Runtime.getRuntime().exec("cmd /c java -version");
            pro.waitFor();
           
            BufferedInputStream br = new BufferedInputStream(pro.getInputStream());
            BufferedInputStream br2 = new BufferedInputStream(pro.getErrorStream());
            int ch;
            System.out.println("Input Stream:");
            while((ch = br.read())!= -1){
                System.out.print((char)ch);
            }
            System.out.println("Error Stream:");
            while((ch = br2.read())!= -1){
                System.out.print((char)ch);
            }
          
        } catch (IOException e) {
            e.printStackTrace();
        }catch (InterruptedException e2) {
            e2.printStackTrace();
        }
    }

你可能感兴趣的:(java,cmd,执行结果)