java - 从 java Runtime.exec 读取流

标签 java inputstream 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

希望对您有所帮助。

关于java - 从 java Runtime.exec 读取流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3343066/

相关文章:

java - 将我的字节数组读入输入流

在 Linux 问题上使用 ssh 的 Java Runtime.exec()

Java运行时,在关闭应用程序时执行一些操作

java - 将 VCF 文件映射到 POJO

java - 线程中断和ActionListener Java

C++:使用 boolalpha

java - 无法从 Runtime.getRunTime.exec() 获取 getInputStream

java - Object[] 不能转换为 resultado[]

java - SharedPreferences.Editor NPE

java - FileInputStream 在skip()调用后留下奇怪的垃圾文本