从Java执行可执行的命令行
生活随笔
收集整理的這篇文章主要介紹了
从Java执行可执行的命令行
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在本文中,我們將介紹Java開發人員的常見需求。 從Java內部執行和管理外部流程。 由于這項任務很常見,因此我們著手尋找一個Java庫來幫助我們完成它。
該庫的要求是:
- 異步執行該過程。
- 能夠中止流程執行。
- 等待流程完成的能力。
- 處理中的輸出通知。
- 能夠在進程掛起時終止進程。
- 獲取流程退出代碼。
本地JDK并沒有太大幫助。 幸運的是,我們有Apache Commons Exec 。 確實,這要容易得多,但仍不如我們希望的那樣簡單。 我們在上面寫了一個小包裝紙。
這是我們公開的方法簽名:
1: public static Future<Long> runProcess(final CommandLine commandline, final ProcessExecutorHandler handler, final long watchdogTimeout) throws IOException;而已! 這是方法植入。 請享用。
import org.apache.commons.exec.*;import org.apache.commons.exec.Executor;import java.io.IOException;import java.util.concurrent.*;public class ProcessExecutor {public static final Long WATCHDOG_EXIST_VALUE = -999L;public static Future<Long> runProcess(final CommandLine commandline, final ProcessExecutorHandler handler, final long watchdogTimeout) throws IOException{ExecutorService executor = Executors.newSingleThreadExecutor();return executor.submit(new ProcessCallable(watchdogTimeout, handler, commandline));}private static class ProcessCallable implements Callable<Long>{private long watchdogTimeout;private ProcessExecutorHandler handler;private CommandLine commandline;private ProcessCallable(long watchdogTimeout, ProcessExecutorHandler handler, CommandLine commandline) {this.watchdogTimeout = watchdogTimeout;this.handler = handler;this.commandline = commandline;}@Overridepublic Long call() throws Exception {Executor executor = new DefaultExecutor();executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());ExecuteWatchdog watchDog = new ExecuteWatchdog(watchdogTimeout);executor.setWatchdog(watchDog);executor.setStreamHandler(new PumpStreamHandler(new MyLogOutputStream(handler, true),new MyLogOutputStream(handler, false)));Long exitValue;try {exitValue = new Long(executor.execute(commandline));} catch (ExecuteException e) {exitValue = new Long(e.getExitValue());}if(watchDog.killedProcess()){exitValue =WATCHDOG_EXIST_VALUE;}return exitValue;}}private static class MyLogOutputStream extends LogOutputStream{private ProcessExecutorHandler handler;private boolean forewordToStandardOutput;private MyLogOutputStream(ProcessExecutorHandler handler, boolean forewordToStandardOutput) {this.handler = handler;this.forewordToStandardOutput = forewordToStandardOutput;}@Overrideprotected void processLine(String line, int level) {if (forewordToStandardOutput){handler.onStandardOutput(line);}else{handler.onStandardError(line);}}}public static void main(String[] args) throws IOException {CommandLine cl = CommandLine.parse("test.bat");Future<Long> exitValue = runProcess(cl, new ProcessExecutorHandler() {@Overridepublic void onStandardOutput(String msg) {System.out.println("output msg = " + msg);}@Overridepublic void onStandardError(String msg) {System.out.println("error msg = " + msg);}}, 1);try {Long aVoid = exitValue.get();System.out.println("Finished with " + aVoid);} catch (InterruptedException e) {e.printStackTrace(); //To change body of catch statement use File | ngs | File Templates.} catch (ExecutionException e) {e.printStackTrace(); //To change body of catch statement use File | ngs | File Templates.}}}參考:在DeveloperLife博客上,從我們的JCG合作伙伴 Nadav Azaria 執行可從Java執行的命令行 。
翻譯自: https://www.javacodegeeks.com/2013/01/executing-a-command-line-executable-from-java.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的从Java执行可执行的命令行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大额存单会自动转存吗?
- 下一篇: Java大新闻不断涌现:Java SE