Java Reference
In-Depth Information
23.2.3.
ProcessBuilder
The
exec
methods of
Runtime
are convenience methods that use the more
general
ProcessBuilder
class.
ProcessBuilder
encapsulates the three key
attributes of an external process: the command, the environment, and
the current working directory. You can then start an external process
with those attributes by using the
ProcessBuilder
object's
start
method-
which returns a
Process
object, just as
exec
does.
ProcessBuilder
has two constructors: One takes a sequence of
String
ob-
jects that represent the command; the other a
List<String>
that repres-
ents the command.
A fourth attribute you can control with
ProcessBuilder
, but not with
exec
,
is whether the standard error stream of the process is redirected to
match the standard output stream of the process. Redirecting the error
stream merges all output into one stream, which makes it much easier
to correlate when errors occur relative to normal processing.
ProcessBuilder
provides methods to query and set each of the four at-
tributes. Each query method simply names the attribute and returns
its valuefor example,
command
returns the current command as a
List<String>
. The other query methods are
public File
directory()
Returns the working directory.
public Map<String, String>
environment()
Returns a map containing all the environment variable set-
tings
public boolean
redirectErrorStream()
Returns
true
if the standard error stream should be redirected
to the standard output stream.