Java running external program (Apache Commons Exec)

Source: Internet
Author: User

Before using Runtime.getruntime (). Exec calls an external program. Under Tomcat there is a phenomenon where the current thread waits.

In order to resolve the problem, the new thread was used to receive output information from the external program. Please see 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 method call external program.

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-clogging method provided in the official tutorial is not applicable in version 1.3


Commons exec encapsulates the invocation of an external program, which requires a small amount of code to implement an external program call. such as running 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 to run exit value of 1, If the command runs successfully and there is no error, return 1executor.setexitvalue (1); int exitvalue = Executor.execute (cmdline);

Commons exec supports building commands by adding a number of parameters. Running the command "acrord32.exe/p/h C:\help.pdf" can also be created as in the following ways.

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 commands to run, assuming that the time exceeded and so on is interrupted.

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 runs Executewatchdog watchdog = new Executewatchdog (60*1000); Executor.setwatchdog (watchdog); Executor.setexitvalue (1); int exitvalue = Executor.execute (cmdline);

The above running external commands are blocked. That is, when you run an external command, the current thread is blocked. Suppose you don't want to block the current thread while running an external command, and you can use Defaultexecuteresulthandler to handle the results of running external commands. Frees 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 such as 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 in the system

Java running 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.