天天看點

java-程式運作外部的exe程式

使用java程式來運作外部的exe程式

java提供了調用外部程式的api

見執行個體:

String[] arguments={"keytool","-gentool","-alias","server","-keystore","server.keystore"};

Process process=Runtime.getRuntime().exec(arguments);

下面包裝一個 運作外部程式的類  獲得輸入的指令 執行 輸出

 public class CommandRunner extends Thread {

        // Private instance variables

    private String[] arguments;

    private StringBuffer outBuf = new StringBuffer();

    private Process process;

    private boolean includeOutputOnException;

    public CommandRunner(String[] arguments) {

        this.arguments = arguments;

    }

    public CommandRunner(Vector arguments) {

        this.arguments = new String[arguments.size()];

        arguments.copyInto(this.arguments);

    }

    public String getOutput() {

        return outBuf.toString();

    }

    public void runCommand() throws Exception {

        try {

                StringBuffer debug = new StringBuffer("Running command '");

                for (int i = 0; i < arguments.length; i++) {

                    if (i > 0) {

                        debug.append(" ");

                    }

                    debug.append("/"");

                    debug.append(arguments[i]);

                    debug.append("/"");

                }

                if(log.isDebugEnabled()) {

                 log.debug(debug.toString());

                }

            process = Runtime.getRuntime().exec(arguments);

            InputStream in = process.getInputStream();

            start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line = null;

            while ((line = reader.readLine()) != null) {

                synchronized (outBuf) {

                    if (outBuf.length() > 0) {

                        outBuf.append("/n");

                    }

                    outBuf.append(line);

                }

            }

            process.waitFor();

            if (process.exitValue() != 0) {

                log.error("Command '" + arguments[0] + "' failed with exit code " + process.exitValue());

                log.error("Start of command output ---->");

                log.error(outBuf.toString());

                log.error("<---- End of command output");

                throw new Exception("Command returned exit code " + process.exitValue() + ". "

                                + (includeOutputOnException ? outBuf.toString() : "Check the logs for more detail."));

            }

        } finally {

        }

    }

    public void setIncludeOutputOnException(boolean includeOutputOnException) {

        this.includeOutputOnException = includeOutputOnException;

    }

    public void run() {

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            String line = null;

            while ((line = reader.readLine()) != null) {

                synchronized (outBuf) {

                    if (outBuf.length() > 0) {

                        outBuf.append("/n");

                    }

                    outBuf.append(line);

                }

            }

        } catch (IOException ioe) {

        }

    }

}

這裡包裝一個類 用于執行外部程式 并獲得執行結果到outBuf之中

注意:取得退出結果(exitValue())的時候,必須等待被調用的程式執行完畢之後才可以取得結果,使用process.waitFor() 方法來等待,該方法阻塞了目前調用程序

并等待被調用程序執行完畢傳回退出結果。

取得執行的結果和錯誤 ErrorStream,使用getInputstream即可 在程式運作後即可取得。上面使用了新開線程方式來取得錯誤。