Java Reference
In-Depth Information
The setting methods have the same names as the query methods,
but they take suitable arguments for the attribute and return the
Pro-
cessBuilder
object. However, there is no method to set the environment.
Instead, the map returned by
environment
can be modified directly and
that will affect the environment of any process subsequently started.
Once a process has been started, any subsequent changes to the attrib-
utes of the
ProcessBuilder
that started it have no effect on that process.
By returning the
ProcessBuilder
object different calls to set attributes can
be chained together and ultimately
start
invoked. For example, here is
how we could rewrite the
ls
example, but with the error stream redirec-
ted:
public String[] ls(String dir, String opts)
throws LSFailedException
{
try {
// start up the command
Process child =
new ProcessBuilder("/bin/ls", opts, dir).
redirectErrorStream(true).start();
// ... as before ...
}
23.2.4. Portability
Any program that uses
exec
or
ProcessBuilder
is not portable across all
systems. Not all environments have processes, and those that have
them can have widely varying commands and syntax for invoking those
commands. Also, because running arbitrary commands raises serious
security issues, the system may not allow all programs to start pro-
cesses. Be very cautious about choosing to do this because it has a
strong negative impact on your ability to run your program everywhere.