Java 运行shell命令然后读取结果

于 2019-06-13 发布

Execute an external program and capture the output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String argv[]) {  
    try {  
      String line;  
      Process p = Runtime.getRuntime().exec  
        (System.getenv("windir") +"\\system32\\"+"tree.com /A");  
      BufferedReader input =  
        new BufferedReader  
          (new InputStreamReader(p.getInputStream()));  
      while ((line = input.readLine()) != null) {  
        System.out.println(line);  
      }  
      input.close();  
    }  
    catch (Exception err) {  
      err.printStackTrace();  
    }  
  }

目录