Java Execution External Program (Apache Commons Exec)

Source: Internet
Author: User
Tags wkhtmltopdf

Before using Runtime.getruntime (). Exec calls an external program, and under Tomcat there is a phenomenon where the current thread waits. In order to solve this problem, using the new thread to receive output information from external programs, see the blog http://blog.csdn.net/accountwcx/article/details/46785437 for details.


Later on the internet found open source Java call external Program class library Apache Commons exce, this class library provides non-blocking methods to invoke external programs.

Official website http://commons.apache.org/proper/commons-exec/

Maven Address http://mvnrepository.com/artifact/org.apache.commons/commons-exec/1.3

Official tutorial http://commons.apache.org/proper/commons-exec/tutorial.html The non-blocking method provided in the official tutorial is not applicable in version 1.3


Commons exec encapsulates a call to an external program, requiring only a small amount of code to implement external program calls, such as executing the command "acrord32.exe/p/h c:\help.pdf".

String line = "acrord32.exe/p/h c:\help.pdf"; CommandLine cmdline = Commandline.parse (line);D Efaultexecutor executor = new Defaultexecutor ();//SET command execution exit value of 1, If the command executes successfully and there is no error, return 1executor.setexitvalue (1); int exitvalue = Executor.execute (cmdline);

Commons exec supports building commands by adding parameters, and executing the command "acrord32.exe/p/h C:\help.pdf" can also be created as follows.

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);D Efaultexecutor executor = new Defaultexecutor (); Executor.setexitvalue (1); int Exitvalue = Executor.execute (cmdline);

Commons exec supports setting the wait time for external command execution and interrupts execution if the time is exceeded and so on.

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);D Efaultexecutor executor = new Defaultexecutor ();//Create a monitoring time of 60 seconds, Over 60 seconds The midrange executes executewatchdog watchdog = new Executewatchdog (60*1000); Executor.setwatchdog (watchdog); Executor.setexitvalue (1); int exitvalue = Executor.execute (cmdline);

The above execution external command is blocked, that is, when the external command is executed, the current thread is blocked. If you do not want to block the current thread while executing the external command, you can use Defaultexecuteresulthandler to handle the results of external command execution and release the current thread.

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);D Efaultexecuteresulthandler resulthandler = new Defaultexecuteresulthandler ();D Efaultexecutor executor = new Defaultexecutor () Executor.setexitvalue (1); Executor.execute (CmdLine, Resulthandler); Resulthandler.waitfor ();


Blog http://blog.csdn.net/accountwcx/article/details/46785437 Htmltopdf class can be changed to the following.

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 The path in the system private static final String Topdftool = "C:\\wkhtmltopdf.exe";/** * @param srcpath HTML path, Can be a local hard drive path or URL * @param destpath PDF Save path * @return Conversion successfully returns True */public static Boolean convert (String Srcpath, String destpa Th) {File File = new file (destpath); File parent = File.getparentfile ()//If the PDF save path does not exist, create the path if (!parent.exists ()) {parent.mkdirs ();} CommandLine cmdline = new CommandLine (topdftool); Cmdline.addargument (Srcpath, True); Cmdline.addargument (DestPath, true);D Efaultexecutor executor = new Defaultexecutor ();//Set the execution command to a successful exit value of 1executor.setexitvalue (1);// Non-blocking Defaultexecuteresulthandler Resulthandler = new Defaultexecuteresulthandler (); Boolean result = True;try { Executor.execute (CmdLine, Resulthandler); Resulthandler.waitfor ();} catch (Exception e) {result = False;e.printstacktrace ();} Return result;}} 



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Execution External Program (Apache Commons Exec)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.