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

之前使用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"。

[java] view plain copy
  1. String line = "AcroRd32.exe /p /h c:\help.pdf";  
  2. CommandLine cmdLine = CommandLine.parse(line);  
  3. DefaultExecutor executor = new DefaultExecutor();  
  4.   
  5. //設置命令執行退出值爲1,如果命令成功執行並且沒有錯誤,則返回1  
  6. executor.setExitValue(1);  
  7.   
  8. int exitValue = executor.execute(cmdLine);  

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

[java] view plain copy
  1. CommandLine cmdLine = new CommandLine("AcroRd32.exe");  
  2. cmdLine.addArgument("/p");  
  3. cmdLine.addArgument("/h");  
  4.   
  5. Map map = new HashMap();  
  6. map.put("file"new File("c:\help.pdf"));  
  7. cmdLine.addArgument("${file}");  
  8. cmdLine.setSubstitutionMap(map);  
  9.   
  10. DefaultExecutor executor = new DefaultExecutor();  
  11. executor.setExitValue(1);  
  12. int exitValue = executor.execute(cmdLine);  

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

[java] view plain copy
  1. CommandLine cmdLine = new CommandLine("AcroRd32.exe");  
  2. cmdLine.addArgument("/p");  
  3. cmdLine.addArgument("/h");  
  4.   
  5. Map map = new HashMap();  
  6. map.put("file"new File("c:\help.pdf"));  
  7. cmdLine.addArgument("${file}");  
  8. cmdLine.setSubstitutionMap(map);  
  9.   
  10. DefaultExecutor executor = new DefaultExecutor();  
  11.   
  12. //創建監控時間60秒,超過60秒則中端執行  
  13. ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);  
  14. executor.setWatchdog(watchdog);  
  15.   
  16. executor.setExitValue(1);  
  17. int exitValue = executor.execute(cmdLine);  

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

[java] view plain copy
  1. CommandLine cmdLine = new CommandLine("AcroRd32.exe");  
  2. cmdLine.addArgument("/p");  
  3. cmdLine.addArgument("/h");  
  4.   
  5. Map map = new HashMap();  
  6. map.put("file"new File("c:\help.pdf"));  
  7. cmdLine.addArgument("${file}");  
  8. cmdLine.setSubstitutionMap(map);  
  9.   
  10. DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();  
  11. DefaultExecutor executor = new DefaultExecutor();  
  12. executor.setExitValue(1);  
  13. executor.execute(cmdLine, resultHandler);  
  14. resultHandler.waitFor();  


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

[java] view plain copy
  1. import java.io.File;  
  2.   
  3. import org.apache.commons.exec.CommandLine;  
  4. import org.apache.commons.exec.DefaultExecuteResultHandler;  
  5. import org.apache.commons.exec.DefaultExecutor;  
  6.   
  7. public class HtmlToPdf {  
  8.     //wkhtmltopdf在系統中的路徑  
  9.     private static final String toPdfTool = "c:\\wkhtmltopdf.exe";  
  10.       
  11.     /** 
  12.      * @param srcPath html路徑,可以本地硬盤路徑或者url 
  13.      * @param destPath pdf保存路徑 
  14.      * @return 轉換成功返回true 
  15.      */  
  16.     public static boolean convert(String srcPath, String destPath){       
  17.         File file = new File(destPath);  
  18.         File parent = file.getParentFile();  
  19.         //如果pdf保存路徑不存在,則創建路徑  
  20.         if(!parent.exists()){  
  21.             parent.mkdirs();  
  22.         }  
  23.           
  24.         CommandLine cmdLine = new CommandLine(toPdfTool);  
  25.         cmdLine.addArgument(srcPath, true);  
  26.         cmdLine.addArgument(destPath, true);  
  27.           
  28.         DefaultExecutor executor = new DefaultExecutor();  
  29.           
  30.         //設置執行命令成功的退出值爲1  
  31.         executor.setExitValue(1);  
  32.           
  33.         //非阻塞  
  34.         DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();  
  35.           
  36.         boolean result = true;  
  37.         try {  
  38.             executor.execute(cmdLine, resultHandler);  
  39.             resultHandler.waitFor();  
  40.         } catch (Exception e) {  
  41.             result = false;  
  42.             e.printStackTrace();  
  43.         }  
  44.           
  45.         return result;  
  46.     }  
  47. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章