Java Reference
In-Depth Information
Figure 24-1. ExecDemoNS in action
A newer class,
ProcessBuilder
, replaces most nontrivial uses of
Runtime.exec()
. This
ProcessBuilder
uses Generic Collections to let you modify or replace the environment, as
shown in
Example 24-2
.
Example 24-2. ProcessBuilderDemo.java
List
<
String
>
command
=
new
new
ArrayList
<>();
command
.
add
(
"notepad"
);
command
.
add
(
"foo.txt"
);
ProcessBuilder builder
=
new
new
ProcessBuilder
(
command
);
builder
.
environment
().
put
(
"PATH"
,
"/windows;/windows/system32;/winnt"
);
final
final
Process godot
=
builder
.
directory
(
new
new
File
(
System
.
getProperty
(
"user.home"
))).
start
();
System
.
err
.
println
(
"Waiting for Godot"
);
godot
.
waitFor
();
Set up the command-line argument list: editor program name and filename.
Co
nfigure the builder's environment to a list of common MS Windows directories.