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

Execute an external program and capture the output

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();  
    }  
  }  

留下评论