Java執行外部程式(Apache Commons Exec)
阿新 • • 發佈:2019-01-05
之前使用Runtime.getRuntime().exec呼叫外部程式,在Tomcat下會有當前執行緒一直等待的現象。當時為了解決這個問題,使用新建執行緒接收外部程式的輸出資訊,詳情請看部落格http://blog.csdn.net/accountwcx/article/details/46785437。
後來在網上找到開源的Java呼叫外部程式類庫Apache Commons Exce,這個類庫提供非阻塞方法呼叫外部程式。
Commons Exec對呼叫外部程式進行了封裝,只需要少量程式碼即可實現外部程式呼叫,如執行命令"AcroRd32.exe /p /h c:\help.pdf"。
-
String line =
- CommandLine cmdLine = CommandLine.parse(line);
- DefaultExecutor executor = new DefaultExecutor();
- //設定命令執行退出值為1,如果命令成功執行並且沒有錯誤,則返回1
- executor.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();
- import java.io.File;
- import org.apache.commons.exec.CommandLine;
- import org.apache.commons.exec.DefaultExecuteResultHandler;
- import org.apache.commons.exec.DefaultExecutor;
- publicclass HtmlToPdf {
- //wkhtmltopdf在系統中的路徑
- privatestaticfinal String toPdfTool = "c:\\wkhtmltopdf.exe";
- /**
- * @param srcPath html路徑,可以本地硬碟路徑或者url
- * @param destPath pdf儲存路徑
- * @return 轉換成功返回true
- */
- publicstaticboolean 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();
- //設定執行命令成功的退出值為1
- executor.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;
- }
- }