THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

In the ls method we want to treat the output as character data, so we wrap the input stream that lets us read
the child's output through an InputStreamReader. If we wanted to treat the child's output as a stream of
bytes, we could easily do that instead. If the example were written to use the second form of exec, the code
would look like this:


String cmd = "/bin/ls " + opts + " " + dir;
Process child = Runtime.getRuntime().exec(cmd);


Process is an abstract class. Each implementation of a Java virtual machine may provide one or more
appropriate extended classes of Process that can interact with processes on the underlying system. Such
classes might have extended functionality that would be useful for programming on the underlying system.
The local documentation should contain information about this extended functionality.


Note that there is no requirement that the child process execute asynchronously or concurrently with respect to
the parent processin a particular system exec could appear to block until the child process terminates.


Exercise 23.1: Write the plugTogether method. You will need threads.


23.2.2. Process Environments


Two other forms of Runtime.exec enable you to specify a set of environment variables, which are
system-dependent values that can be queried as desired by the new process. Environment variables are passed
to exec as a String array; each element of the array specifies the name and value of an environment
variable in the form name=value. The name cannot contain any spaces, although the value can be any
string. The environment variables are passed as the second parameter:


public Process exec(String[] cmdArray, String[] env)
throws IOException
public Process exec(String command, String[] env)
throws IOException


The single argument forms of exec are equivalent to passing null for env, which means that the created
process inherits the environment variables of its parent.


Environment variables are interpreted in a system-dependent way by the child process's program. They can
hold information such as the current user name, the current working directory, search paths, or other useful
information that may be needed by a running program. The environment variables mechanism is supported
because existing programs on many different kinds of platforms understand them. You can get the
environment variables of the current runtime process from the System.getenv method, which returns an
unmodifiable map of all the name/value pairs. Individual values can be retrieved using the System.getenv
method which takes a string argument representing the name of the environment variable. The preferred way
to communicate between different virtual machine runtimes is with system properties (see page 663).
However, this remains the only means of querying and subsequently modifying the environment when
executing a non-Java program.


There remain two further forms of exec that allow the initial working directory of the child process to be
specified:


public Process exec(String[] cmdArray, String[] env, File dir)
throws IOException
public Process exec(String command, String[] env, File dir)

Free download pdf