From Java runtime Exec read stream
•
Java
I have the following code snippets:
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 are runnables that use BufferedReader to read the input and error stream of the execution process Although this works most of the time, I occasionally see a window (about 2 minutes). I set processexitcode to 0, which indicates normal termination, but there is nothing in the input and error streams - not even anything
As I said before, this works most of the time, but this failure happens every once in a while - I'm completely confused Any ideas?
rag
Solution
I've been trying to solve the same problem
/** * 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
I hope it helps
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码