Java Reference
In-Depth Information
23.2.1.
Process
The
exec
methods return a
Process
object for each child process created.
This object represents the child process in two ways. First, it provides
methods to get input, output, and error streams for the child process:
[1]
[1]
For historical reasons these are byte stream objects instead of character streams. You can use
In-
putStreamReader
and
OutputStreamWriter
to convert the bytes to characters under the system default
encoding or another of your choosing.
public abstract OutputStream
getOutputStream()
Returns an
OutputStream
connected to the standard input of the
child process. Data written on this stream is read by the child
process as its input.
public abstract InputStream
getInputStream()
Returns an
InputStream
connected to the standard output of
the child process. When the child writes data on its output, it
can be read from this stream.
public abstract InputStream
getErrorStream()
Returns an
InputStream
connected to the error output stream
of the child process. When the child writes data on its error
output, it can be read from this stream.
Here, for example, is a method that connects the standard streams of
the parent process to the standard streams of the child process so that
whatever the user types will go to the specified program and whatever
the program produces will be seen by the user:
public static Process userProg(String cmd)
throws IOException
{
Process proc = Runtime.getRuntime().exec(cmd);