java執行cmd命令的兩種方法


一般java在執行CMD命令時,通常是使用Runtime.getRuntime.exec(command)來執行的,這個方法有兩種細節要注意:

1.一般執行方法,代碼如下,這種方法有時執行exe時會卡在那里。

 1 //一般的執行方法,有時執行exe會卡在那    stmt要執行的命令
 2     public static void executive(String stmt) throws IOException, InterruptedException {
 3         Runtime runtime = Runtime.getRuntime();  //獲取Runtime實例
 4         //執行命令
 5         try {
 6             String[] command = {"cmd", "/c", stmt};
 7             Process process = runtime.exec(command);
 8             // 標准輸入流(必須寫在 waitFor 之前)
 9             String inStr = consumeInputStream(process.getInputStream());
10             // 標准錯誤流(必須寫在 waitFor 之前)
11             String errStr = consumeInputStream(process.getErrorStream()); //若有錯誤信息則輸出
14 int proc = process.waitFor(); 16 if (proc == 0) { 17 System.out.println("執行成功"); 18 } else { 19 System.out.println("執行失敗" + errStr); 20 } 21 } catch (IOException | InterruptedException e) { 22 e.printStackTrace(); 23 } 24 } 25 26   /** 27 * 消費inputstream,並返回 28 */ 29 public static String consumeInputStream(InputStream is) throws IOException { 30 BufferedReader br = new BufferedReader(new InputStreamReader(is,"GBK")); 31 String s; 32 StringBuilder sb = new StringBuilder(); 33 while ((s = br.readLine()) != null) { 34 System.out.println(s); 35 sb.append(s); 36 } 37 return sb.toString(); 38 }

 

 

2.第二種方法是先生成一個緩存文件,用來緩存執行過程中輸出的信息,這樣在執行命令的時候不會卡。代碼如下:

 

 1   //這個方法比第一個好,執行時不會卡  stmt要執行的命令
 2     public static void aaa(String stam){
 3         BufferedReader br = null;
 4         try {
 5             File file = new File("D:\\daemonTmp");
 6             File tmpFile = new File("D:\\daemonTmp\\temp.tmp");//新建一個用來存儲結果的緩存文件
 7             if (!file.exists()){
 8                 file.mkdirs();
 9             }
10             if(!tmpFile.exists()) {
11                 tmpFile.createNewFile();
12             }
13             ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", stam).inheritIO();
14             pb.redirectErrorStream(true);//這里是把控制台中的紅字變成了黑字,用通常的方法其實獲取不到,控制台的結果是pb.start()方法內部輸出的。
15             pb.redirectOutput(tmpFile);//把執行結果輸出。
16             pb.start().waitFor();//等待語句執行完成,否則可能會讀不到結果。
17             InputStream in = new FileInputStream(tmpFile);
18             br= new BufferedReader(new InputStreamReader(in));
19             String line = null;
20             while((line = br.readLine()) != null) {
21                 System.out.println(line);
22             }
23             br.close();
24             br = null;
25             tmpFile.delete();//卸磨殺驢。
26             System.out.println("執行完成");
27         } catch (Exception e) {
28             e.printStackTrace();
29         } finally {
30             if(br != null) {
31                 try {
32                     br.close();
33                 } catch (IOException e) {
34                     e.printStackTrace();
35                 }
36             }
37         }
38     }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM