執行外部程序的Java類庫:Apache Commons Exec 1.3 發布

jopen 10年前發布 | 13K 次閱讀 Apache Commons Exec

Apache Commons Exec 1.3 發布了,Apache Commons Exec 是 Apache 上的一個 Java 項目,提供一些常用的方法用來執行外部進程。

改進記錄包括:

新特性:
o DefaultExecutor async execute prevents shutdown hooks running.

Bug 修復:
o Remove remaining raw types, unchecked conversions
o NPE in EnvironmentUtils.toString(map)

改動:
o No need to use System.class.getMethod("getenv",...) any more
o Update JUnit dependency to 4.11
o Update to Java 5

利用Apache Commons Exec調用命令行并取得命令行的輸出(實例)

    public String ping(String ip) {

        try {  

            String command = "ping "+ip;  

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  

            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();  

            CommandLine commandline = CommandLine.parse(command);  

            DefaultExecutor exec = new DefaultExecutor();  

            exec.setExitValues(null);  

            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);  

            exec.setStreamHandler(streamHandler);  

            exec.execute(commandline);  

            String out = outputStream.toString("gbk");  

            String error = errorStream.toString("gbk");  

            return out+error;  

        } catch (Exception e) {  

            log.error("ping task failed.",e);  

            return e.toString();  

        }  

    }  </pre><p></p>

項目地址:http://commons.apache.org/proper/commons-exec/


來自:http://www.oschina.net/news/56823/apache-commons-exec-1-3

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!