簡體   English   中英

從java Runtime.exec讀取流

[英]Reading streams from java Runtime.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是Runnable的,它使用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

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM