I am implementing a mailx command through Runtime.exec and came across this article which explains the right way of doing it.
I have been through the examples and see that they have introduced a new class StreamGobbler which accepts the InputStream and prints the output.
However I fail to understand the reason why this has been introduced. Can anybody please explain.
Also as part of my code , I have written the following
OutputStreamWriter osw = new OutputStreamWriter(proc.getOutputStream())
osw.write(mailBody)
osw.close
Is this implementation correct or there are any pitfalls to it ?
Since your program can potentially succeed (which means the program’s output would be available on the
Process'getInputStream()) or it can potentially fail (which means the program’s error would be available on theProcess'getErrorStream(), you should be able to read these in parallel and hence the need for a separate thread which is implemented by theStreamGobblerclass. Hope this clarifies to an extent.