java process調用可執行檔(windows 或者 linux)

來源:互聯網
上載者:User

最近項目中看到有需要在java程式中調用其他三方工具(如程式中調用三方轉碼工具)的需求,於是process便在這個需求中嶄露頭角:

(一下只簡述用法,不透露實際應用)

1.調用windows平台的.bat

a.在F:盤下 建立a.txt 編輯內容為“love code”;

     建立cmd.bat,編輯內容“notepad F:\a.txt”

b.在java程式中寫如下調用語句

 

CommandUtil.exec("cmd /c start F://cmd.bat");

 

 

CommandUtil的exec方法如下:

 

public static boolean exec(String command) throws IOException, InterruptedException{log.info("執行指令碼:"+command);Process process = Runtime.getRuntime().exec(command);int exitValue = process.waitFor(); if (process != null) {            process.destroy();        } if (exitValue!=0){ return false; } return true;}

c.在windows平台下運行程式,變可以開啟記事本,看到“love code”字樣;

 

2.調用linux平台下的.sh指令碼

用法一樣,只是把bat檔案換成linux下的shell指令檔,並用相同代碼調用 。

在linux平台下運行程式即可調用shell指令碼。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.