加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

从java Runtime.exec读取流

发布时间:2020-12-15 04:52:49 所属栏目:Java 来源:网络整理
导读:我有以下代码片段: Process proc = runtime.exec(command);errorGobbler = new ErrorStreamGobbler(proc.getErrorStream(),logErrors,mdcMap);outputGobbler = new OutputStreamGobbler(proc.getInputStream(),mdcMap);executor.execute(errorGobbler);exec
我有以下代码片段:

Process proc = runtime.exec(command);
errorGobbler = new ErrorStreamGobbler(proc.getErrorStream(),logErrors,mdcMap);
outputGobbler = new OutputStreamGobbler(proc.getInputStream(),mdcMap);
executor.execute(errorGobbler);
executor.execute(outputGobbler);
processExitCode = proc.waitFor();

gobblers是Runnables,它使用BufferedReader来读取执行进程的输入和错误流.虽然这大部分时间都有效,但我偶尔会看到窗口(大约2分钟左右),我将processExitCode设为0,这表示正常终止,但输入和错误流中没有任何内容 – 甚至没有任何内容表示 – 的流.

就像我之前说过的那样,这种情况大部分时间都有效,但这种失败每隔一段时间就会发生 – 我完全感到困惑.有任何想法吗?

碎布

解决方法

我一直在努力解决同样的问题.
我不记得到底出了什么问题(也许我忘了正确地冲洗/关闭流或什么……).
无论如何,这就是我想出的.

/**
 *  Handle communication with a process,reading its output/error and feeding its input
 *  @param process The process to execute
 *  @param _in Reader that will feed the input pipe of the process
 *  @param out Writer that will receive the output of the process
 *  @param err Writer that will receive the error pipe of the process
 */
public static void communicate(
        Process process,final Reader _in,final Writer out,final Writer err)
{
    // Buffer the input reader
    final BufferedReader in = new BufferedReader(_in);

    // Final versions of the the params,to be used within the threads
    final BufferedReader stdOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
    final BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    final BufferedWriter stdIn = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

    // Thread that reads std out and feeds the writer given in input
    new Thread() {
        @Override public void run() {
            String line;
            try {
                while ((line = stdOut.readLine()) != null) {
                   out.write(line + newline);
                }
            } catch (Exception e) {throw new Error(e);}
            try {
                out.flush();
                out.close();
            } catch (IOException e) { /* Who cares ?*/ }
        }
    }.start(); // Starts now

    // Thread that reads std err and feeds the writer given in input
    new Thread() {
        @Override public void run() {
            String line;
            try {
                while ((line = stdErr.readLine()) != null) {
                    err.write(line + newline);
                }
            } catch (Exception e) {throw new Error(e);}
            try {
                err.flush();
                err.close();
            } catch (IOException e) { /* Who cares ?*/ }
        }
    }.start(); // Starts now

    // Thread that reads the std in given in input and that feeds the input of the process
    new Thread() {
        @Override public void run() {
            String line;
            try {
                while ((line = in.readLine()) != null) {
                    stdIn.write(line + newline);
                }
            } catch (Exception e) {throw new Error(e);}

            try {
                stdIn.flush();
                stdIn.close();
            } catch (IOException e) { /* Who cares ?*/ }
        }
    }.start(); // Starts now

    // Wait until the end of the process
    try {
         process.waitFor();
    } catch (Exception e) {
        throw new Error(e);
    }

} // End of #communicate

我希望这有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读