Java Reference
In-Depth Information
23.2. Creating Processes
As you have learned, a running system can have many threads of ex-
ecution. Most systems that host a Java virtual machine can also run
multiple programs. You can execute new programs by using one of the
Runtime.exec
convenience methods. Each successful invocation of
exec
creates a new
Process
object that represents the program running in its
own
process.
You can use a
Process
object to query the process's state
and invoke methods to control its progress.
Process
is an abstract class
whose subclasses are defined on each system to work with that system's
processes. The two basic forms of
exec
are
public Process
exec(String[] cmdArray)
tHRows IOException
Runs the command in
cmdArray
on the current system. Returns
a
Process
object (described below) to represent it. The string in
cmdArray[0]
is the name of the command, and any subsequent
strings in the array are passed to the command as arguments.
public Process
exec(String command)
tHRows IOException
Equivalent to the array form of
exec
with the string
command
split into an array wherever whitespace occurs, using a default
The newly created process is called a
child
process. By analogy, the cre-
ating process is a
parent
process.
Creating processes is a privileged operation and a
SecurityException
is
thrown if you are not permitted to do it. If anything goes wrong when the
system tries to create the process, an
IOException
is thrown.