日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

从Java执行可执行的命令行

發布時間:2023/12/3 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从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;
  • 它返回一個Future <Long>。 這涵蓋了第1,2,3,6節。
  • ProcessExecutorHandler的實例傳遞給該函數。 該實例實際上是任何進程輸出的偵聽器。 這涵蓋了我們要求中的第4節。
  • 最后但并非最不重要的一點是您要提供超時。 如果進程執行所花的時間超過了上述超時時間,則假定進程已掛起,然后將其終止。 在這種情況下,進程返回的錯誤代碼將為-999。
  • 而已! 這是方法植入。 請享用。

    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执行可执行的命令行的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。