Why is the getErrorStream () process needed to start the process?

I am executing a process using the Process class. Going through a stream of errors seems to be necessary for the process to succeed. Why is the error flow necessary for the process to work properly? Is there something I'm doing wrong?

Process wkstdin = Runtime.getRuntime().exec(command); BufferedWriter wkstdin_writer = new BufferedWriter( new OutputStreamWriter(wkstdin.getOutputStream())); //write data 

Necessary part of the code:

 BufferedReader input = new BufferedReader(new InputStreamReader( wkstdin.getErrorStream())); String ch; while ((ch = input.readLine()) != null) { System.out.println(ch); } 
+4
source share
2 answers

When a process writes to stderr, the output goes to a buffer of a fixed size. If the buffer is full, the process is blocked until there is no space left in the buffer for the remaining output. Therefore, if the buffer is not empty, the process will freeze.

Also, if something goes wrong with the process that you would like to know about, the error stream may contain actual useful information.

Some suggestions:

  • The naming of the string ch seems misleading, since ch is commonly used for characters.
  • I like to put code that is read from stderr in a dedicated workflow. Thus, switching between reading the input stream and the error stream occurs automatically without my permission.
+5
source

It really depends on your target process.

Many times, the target process will block until the threads are consumed ( you must delete the threads of the target process thread ). Therefore, if you do not handle error / output streams, then the target process may appear to hang because it is blocked for the stream to be deleted.

+2
source

All Articles