Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Runtime


TheRuntimeclass encapsulates the run-time environment. You cannot instantiate aRuntime
object. However, you can get a reference to the currentRuntimeobject by calling the static
methodRuntime.getRuntime( ). Once you obtain a reference to the currentRuntimeobject,
you can call several methods that control the state and behavior of the Java Virtual Machine.
Applets and other untrusted code typically cannot call any of theRuntimemethods without
raising aSecurityException. Commonly used methods defined byRuntimeare shown in
Table 16-11.

404 Part II: The Java Library


Method Description
void addShutdownHook(Threadthrd) Registersthrdas a thread to be run when the Java
Virtual Machine terminates.
Process exec(StringprogName)
throws IOException

Executes the program specified byprogNameas a
separate process. An object of typeProcessis
returned that describes the new process.
Process exec(StringprogName,
Stringenvironment[ ])
throws IOException

Executes the program specified byprogNameas a
separate process with the environment specified by
environment.An object of typeProcessis returned
that describes the new process.
Process exec(StringcomLineArray[ ])
throws IOException

Executes the command line specified by the strings in
comLineArrayas a separate process. An object of type
Processis returned that describes the new process.
Process exec(StringcomLineArray[ ],
Stringenvironment[ ])
throws IOException

Executes the command line specified by the strings
incomLineArrayas a separate process with the
environment specified byenvironment.An object of type
Processis returned that describes the new process.
void exit(intexitCode) Halts execution and returns the value ofexitCodeto
the parent process. By convention, 0 indicates normal
termination. All other values indicate some form of error.
long freeMemor y( ) Returns the approximate number of bytes of free
memor y available to the Java run-time system.
void gc( ) Initiates garbage collection.
static Runtime getRuntime( ) Returns the currentRuntimeobject.
void halt(intcode) Immediately terminates the Java Virtual Machine. No
termination threads or finalizers are run. The value of
codeis returned to the invoking process.
void load(Stringlibrar yFileName) Loads the dynamic librar y whose file is specified by
librar yFileName,which must specify its complete path.
void loadLibrar y(Stringlibrar yName) Loads the dynamic librar y whose name is associated
withlibrar yName.

TABLE 16-11 A Sampling of Methods Defined byRuntime
Free download pdf