Capturing the Output of a Child Process
We now have two different programs; one that can run by itself (Example05), and one that is executed from another one, which will also try to send information to it and capture its output. The purpose of this section is to capture the output from Example05 and print it out to a terminal.
To capture whatever is being sent by the child process to System.out
, we need to create a BufferedReader
in the parent class that will be fed from the InputStream
that can be instantiated from the process. In other words, we need to enhance Example06 with the following:
InputStream in = process.getInputStream(); Reader reader = new InputStreamReader(in); BufferedReader bufferedReader = new BufferedReader(reader); String line = bufferedReader.readLine(); System.out.println(line);
The reason for needing a BufferedReader
is that we are using the end of the line (EOL
or "\n
") as a marker for a message between processes. That allows the utilization...