Чтение потоков из Java Runtime.exec - PullRequest
3 голосов
/ 27 июля 2010

У меня есть следующий фрагмент кода:

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();

, где гоблерами являются Runnable s, которые используют BufferedReader для чтения потоков ввода и ошибок выполняющегося процесса.Хотя это работает большую часть времени, я получаю случайное окно (около 2 минут или около того), где я получаю processExitCode как 0, что указывает на нормальное завершение, но нет ничего в потоках ввода и ошибок - ничего даже не указыватьконец потока.

Как я уже говорил ранее, это работает большую часть времени, но этот сбой происходит время от времени - и я совершенно озадачен.Есть идеи?

Тряпки

1 Ответ

12 голосов
/ 28 июля 2010

Я боролся с такими же проблемами.Я не могу вспомнить, что именно было не так (может быть, я забыл правильно смыть / закрыть потоки или что-то в этом роде ...).В любом случае, вот что я придумала.

/**
 *  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

Надеюсь, это поможет.

...