Java executes the external program (Apache Commons Exec), apachecommons
Use runtime.getruntime(cmd.exe c to call an external program. The current thread remains waiting in Tomcat. To solve this problem, the new thread was used to receive output information from external programs. For more information, see the blog http://blog.csdn.net/accountwcx/article/details/46785437.
Later, I found open-source Java on the Internet to call the external program class library Apache Commons Exce, which provides a non-blocking method to call 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 tutorials http://commons.apache.org/proper/commons-exec/tutorial.html official tutorials provide non-blocking methods not applicable in version 1.3
Commons Exec encapsulates the calling of external programs. You only need a small amount of code to call external programs, such as executing the command "Export rd32.exe/p/h c: \ help.pdf ".
String line = "Export rd32.exe/p/h c: \ help;"; CommandLine export line = CommandLine. parse (line); DefaultExecutor executor = new DefaultExecutor (); // set the exit value of command execution to 1. If the command is successfully executed and there is no error, 1 executor is returned. setExitValue (1); int exitValue = executor.exe cute (cmdLine );
Commons Exec supports building commands by adding parameters. The command "Export rd32.exe/p/h c: \ help;" 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);DefaultExecutor executor = new DefaultExecutor();executor.setExitValue(1);int exitValue = executor.execute(cmdLine);
Commons Exec supports setting the wait time for external command execution. If the wait time is exceeded, the execution is interrupted.
CommandLine using line = new CommandLine ("Export rd32.exe"); using line. addArgument ("/p"); using line. addArgument ("/h"); Map map = new HashMap (); map. put ("file", new File ("c: \ help;"); using line. addArgument ("$ {file}"); using line. setSubstitutionMap (map); DefaultExecutor executor = new DefaultExecutor (); // The monitoring creation time is 60 seconds. If the monitoring time exceeds 60 seconds, the middle end executes ExecuteWatchdog watchdog = new ExecuteWatchdog (60*1000. setWatchdog (watchdog); executor. setExitValue (1); int exitValue = executor.exe cute (cmdLine );
The above External commands are all blocked, that is, when executing external commands, the current thread is blocked. If you do not want to block the current thread when executing external commands, you can use DefaultExecuteResultHandler to process 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);DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();DefaultExecutor executor = new DefaultExecutor();executor.setExitValue(1);executor.execute(cmdLine, resultHandler);resultHandler.waitFor();
Blog Tips.
Import java. io. file; import org.apache.commons.exe c. commandLine; import org.apache.commons.exe c. defaultExecuteResultHandler; import org.apache.commons.exe c. defaultExecutor; public class HtmlToPdf {// wkhtmltopdf path in the System private static final String tow.tool = "c: \ wkhtmltow..exe";/*** @ param srcPath html path, you can save the local hard drive path or url * @ param destPath pdf path * @ return returns true */public static boolean convert (Strin G srcPath, String destPath) {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 using line = new CommandLine (tow.tool); using line. addArgument (srcPath, true); specify line. addArgument (destPath, true); DefaultExecutor executor = new DefaultExecutor (); // set the exit value of the command to 1executor. setExitValue (1); // non-blocking DefaultExecuteResultHandler resultHandler = new handler (); boolean result = true; try restarting executor.exe cute (using line, resultHandler); resultHandler. waitFor ();} catch (Exception e) {result = false; e. printStackTrace ();} return result ;}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.