手机版
你好,游客 登录 注册
背景:
阅读新闻

Java运行系统命令

[日期:2013-05-02] 来源:Linux社区  作者:sheismylife [字体:大 中 小]

最重要的Command.Java代码:

package command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Describe class Command here.
 *
*/
public class Command {

    private long startTime;
    private long timeout;
    private static final long INTERVAL = 500;

    /*
    * command is one system command represented by string
    * timeout is milliseconds for waiting the command execution
    * throw CommandError if the system command can't be executed
    * return CommandResult when the system command is executed no matter what the command returns(succeed or failed)
    */
    public CommandResult exec(final String command, long timeout) throws CommandError {
        this.timeout = timeout;
        try {
            Process process = Runtime.getRuntime().exec(command);
            CommandResult result = wait(process);
            if (process != null) {
                process.destroy();
            }
            return result;
        } catch (Exception ex) {
            throw new CommandError(ex);
        }
    }

    private boolean isTimeout() {
        return System.currentTimeMillis() - startTime >= timeout;
    }

    private CommandResult wait(final Process process) throws InterruptedException, IOException {
        BufferedReader errorStreamReader = null;
        BufferedReader inputStreamReader = null;
        try {
            errorStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            //timeout control
            startTime = System.currentTimeMillis();
            boolean isFinished = false;

            for (;;) {
                if (isTimeout()) {
                    CommandResult result = new CommandResult();
                    result.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
                    result.setOutput("Command process timeout");
                    return result;
                }

                if (isFinished) {
                    CommandResult result = new CommandResult();
                    result.setExitValue(process.waitFor());

                    //parse error info
                    if (errorStreamReader.ready()) {
                        StringBuilder buffer = new StringBuilder();
                        String line;
                        while ((line = errorStreamReader.readLine()) != null) {
                            buffer.append(line);
                        }
                        result.setError(buffer.toString());
                    }

                    if (inputStreamReader.ready()) {
                        StringBuilder buffer = new StringBuilder();
                        String line;
                        while ((line = inputStreamReader.readLine()) != null) {
                            buffer.append(line);
                        }
                        result.setOutput(buffer.toString());
                    }
                    return result;
                }

                try {
                    isFinished = true;
                    process.exitValue();
                } catch (IllegalThreadStateException e) {
                    // process hasn't finished yet
                    isFinished = false;
                    Thread.sleep(INTERVAL);
                }
            }

        } finally {
            if (errorStreamReader != null) {
                try {
                    errorStreamReader.close();
                } catch (IOException e) {
                }
            }

            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

linux
相关资讯       系统命令 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款