java調用Dos命令

來源:互聯網
上載者:User

 比如我在工程檔案夾下放了一個svm-train.exe的檔案

這個exe檔案調用後有輸出資訊。我很想知道這個exe調用過程中到底發生了什麼事情。

在java中這樣寫

  Runtime run = Runtime.getRuntime();  
  Process child = null;
  InputStream is = null;

  File env = new File(System.getProperty("user.dir"));
   child = run.exec(cmd,null,env);

這個env是用於找到工程的目前的目錄,相當於進入Dos後先cd到目錄,cmd就是你在dos視窗那裡打的命令。

run.exec(cmd,null,env);這句就開始執行了。並建立一個Process給child

網上有很多代碼有這句:

 while(true)
   {
    if(child.waitFor() == 0)
    {
     break;
    }

   }

但是注意如果進入 if(child.waitFor() == 0)
那這個程式就會卡死在這裡,直到exe進程結束。這樣如果你的exe會運行很長時間,那麼在這段時間內就不能列印任何輸出資訊。

全代碼如下:

  1.     private static boolean CallSVM(String cmd)
  2.     {
  3.         Runtime run = Runtime.getRuntime();     
  4.         Process child = null;
  5.         InputStream is = null;
  6.         
  7.         try
  8.         {
  9.             File env = new File(System.getProperty("user.dir"));
  10.             
  11.             child = run.exec(cmd,null,env);
  12.             
  13.             String out = null;
  14.         
  15.             
  16.             // nomal process
  17.             is = child.getInputStream();
  18.             FileWriter fw = new FileWriter(env.getAbsolutePath()+"//log.txt");            
  19.             BufferedReader br = new BufferedReader(new InputStreamReader(is));
  20.             
  21.             while((out = br.readLine())!=null)
  22.             {
  23.                 fw.write(out+"/r/n");
  24.                 System.out.println(out);
  25.             }
  26.             while(true)
  27.             {
  28.                 if(child.waitFor() == 0)
  29.                 {
  30.                     break;
  31.                 }
  32.             }
  33.             fw.write("-----------------/r/n");
  34.             
  35.             fw.close();
  36.             br.close();
  37.             
  38.         }
  39.         catch(IOException ioe)
  40.         {
  41.             System.err.println("cmd error:"+cmd);
  42.             System.err.println(ioe.getMessage());
  43.             return false;
  44.         } catch (InterruptedException e) {
  45.             // TODO Auto-generated catch block
  46.             if(child != null)
  47.                 child.destroy();
  48.             e.printStackTrace();
  49.         }
  50.         return true;
  51.     }

這裡一定要輸出放前,waitFor放後

   while((out = br.readLine())!=null)
   {
    fw.write(out+"/r/n");
    System.out.println(out);
   }

 

   while(true)
   {
    if(child.waitFor() == 0)
    {
     break;
    }

   }

另外程式在執行過程中while((out = br.readLine())!=null)這裡也會卡住,所以不用擔心,調用的dos程式還沒有輸出,就已經執行到waitFor()那裡去了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.