Java執行外部程式(Apache Commons Exec)

來源:互聯網
上載者:User

標籤:java   執行外部程式   html轉pdf   

之前使用Runtime.getRuntime().exec調用外部程式,在Tomcat下會有當前線程一直等待的現象。當時為瞭解決這個問題,使用建立線程接收外部程式的輸出資訊,詳情請看部落格http://blog.csdn.net/accountwcx/article/details/46785437。


後來在網上找到開源的Java調用外部程式類庫Apache Commons Exce,這個類庫提供非阻塞方法調用外部程式。

官方網址 http://commons.apache.org/proper/commons-exec/

maven地址 http://mvnrepository.com/artifact/org.apache.commons/commons-exec/1.3

官方教程 http://commons.apache.org/proper/commons-exec/tutorial.html 官方教程提供的非阻塞方法在1.3版中不適用


Commons Exec對調用外部程式進行了封裝,只需要少量代碼即可實現外部程式調用,如執行命令"AcroRd32.exe /p /h c:\help.pdf"。

String line = "AcroRd32.exe /p /h c:\help.pdf";CommandLine cmdLine = CommandLine.parse(line);DefaultExecutor executor = new DefaultExecutor();//設定命令執行退出值為1,如果命令成功執行並且沒有錯誤,則返回1executor.setExitValue(1);int exitValue = executor.execute(cmdLine);

Commons Exec支援通過添加參數方式構建命令,執行命令"AcroRd32.exe /p /h c:\help.pdf"也可以按如下方法建立。

CommandLine cmdLine = new CommandLine("AcroRd32.exe");cmdLine.addArgument("/p");cmdLine.addArgument("/h");Map map = new HashMap();map.put("file", new File("c:\help.pdf"));cmdLine.addArgument("${file}");cmdLine.setSubstitutionMap(map);DefaultExecutor executor = new DefaultExecutor();executor.setExitValue(1);int exitValue = executor.execute(cmdLine);

Commons Exec支援設定外部命令執行等待時間,如果超過等等時間則中斷執行。

CommandLine cmdLine = new CommandLine("AcroRd32.exe");cmdLine.addArgument("/p");cmdLine.addArgument("/h");Map map = new HashMap();map.put("file", new File("c:\help.pdf"));cmdLine.addArgument("${file}");cmdLine.setSubstitutionMap(map);DefaultExecutor executor = new DefaultExecutor();//建立監控時間60秒,超過60秒則中端執行ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);executor.setWatchdog(watchdog);executor.setExitValue(1);int exitValue = executor.execute(cmdLine);

上面的執行外部命令都是阻塞式,也就是在執行外部命令時,當前線程是阻塞的。如果不想在執行外部命令的時候,把當前線程阻塞,可以使用DefaultExecuteResultHandler處理外部命令執行的結果,釋放當前線程。

CommandLine cmdLine = new CommandLine("AcroRd32.exe");cmdLine.addArgument("/p");cmdLine.addArgument("/h");Map map = new HashMap();map.put("file", new File("c:\help.pdf"));cmdLine.addArgument("${file}");cmdLine.setSubstitutionMap(map);DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();DefaultExecutor executor = new DefaultExecutor();executor.setExitValue(1);executor.execute(cmdLine, resultHandler);resultHandler.waitFor();


部落格http://blog.csdn.net/accountwcx/article/details/46785437的HtmlToPdf類可以改成如下。

import java.io.File;import org.apache.commons.exec.CommandLine;import org.apache.commons.exec.DefaultExecuteResultHandler;import org.apache.commons.exec.DefaultExecutor;public class HtmlToPdf {//wkhtmltopdf在系統中的路徑private static final String toPdfTool = "c:\\wkhtmltopdf.exe";/** * @param srcPath html路徑,可以本地硬碟路徑或者url * @param destPath pdf儲存路徑 * @return 轉換成功返回true */public static boolean convert(String srcPath, String destPath){File file = new File(destPath);File parent = file.getParentFile();//如果pdf儲存路徑不存在,則建立路徑if(!parent.exists()){parent.mkdirs();}CommandLine cmdLine = new CommandLine(toPdfTool);cmdLine.addArgument(srcPath, true);cmdLine.addArgument(destPath, true);DefaultExecutor executor = new DefaultExecutor();//設定執行命令成功的退出值為1executor.setExitValue(1);//非阻塞DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();boolean result = true;try {executor.execute(cmdLine, resultHandler);resultHandler.waitFor();} catch (Exception e) {result = false;e.printStackTrace();}return result;}}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java執行外部程式(Apache Commons Exec)

聯繫我們

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