java.lang
public class Runtime extends Object
Method Summary | |
---|---|
void | addShutdownHook(Thread hook)
Register a new shutdown hook. |
int | availableProcessors()
Returns the number of available processors currently available to the
virtual machine. |
Process | exec(String cmdline)
Create a new subprocess with the specified command line. |
Process | exec(String cmdline, String[] env)
Create a new subprocess with the specified command line and environment.
|
Process | exec(String cmdline, String[] env, File dir)
Create a new subprocess with the specified command line, environment, and
working directory. |
Process | exec(String[] cmd)
Create a new subprocess with the specified command line, already
tokenized. |
Process | exec(String[] cmd, String[] env)
Create a new subprocess with the specified command line, already
tokenized, and specified environment. |
Process | exec(String[] cmd, String[] env, File dir)
Create a new subprocess with the specified command line, already
tokenized, and the specified environment and working directory. |
void | exit(int status)
Exit the Java runtime. |
long | freeMemory()
Find out how much memory is still free for allocating Objects on the heap.
|
void | gc()
Run the garbage collector. |
InputStream | getLocalizedInputStream(InputStream in)
Return a localized version of this InputStream, meaning all characters
are localized before they come out the other end.
|
OutputStream | getLocalizedOutputStream(OutputStream out)
Return a localized version of this OutputStream, meaning all characters
are localized before they are sent to the other end.
|
static Runtime | getRuntime()
Get the current Runtime object for this JVM. |
void | halt(int status)
Forcibly terminate the virtual machine. |
void | load(String filename)
Load a native library using the system-dependent filename. |
void | loadLibrary(String libname)
Load a native library using a system-independent "short name" for the
library. |
long | maxMemory()
Returns the maximum amount of memory the virtual machine can attempt to
use. |
boolean | removeShutdownHook(Thread hook)
De-register a shutdown hook. |
void | runFinalization()
Run finalization on all Objects that are waiting to be finalized. |
static void | runFinalizersOnExit(boolean finalizeOnExit)
Tell the VM to run the finalize() method on every single Object before
it exits. |
long | totalMemory()
Find out how much memory total is available on the heap for allocating
Objects.
|
void | traceInstructions(boolean on)
Tell the VM to trace every bytecode instruction that executes (print out
a trace of it). |
void | traceMethodCalls(boolean on)
Tell the VM to trace every method call that executes (print out a trace
of it). |
System.exit
was invoked), or when the user terminates
the virtual machine (such as by typing ^C, or logging off). There is
a security check to add hooks,
RuntimePermission("shutdownHooks")
.
The hook must be an initialized, but unstarted Thread. The threads
are run concurrently, and started in an arbitrary order; and user
threads or daemons may still be running. Once shutdown hooks have
started, they must all complete, or else you must use halt
,
to actually finish the shutdown sequence. Attempts to modify hooks
after shutdown has started result in IllegalStateExceptions.
It is imperative that you code shutdown hooks defensively, as you do not want to deadlock, and have no idea what other hooks will be running concurrently. It is also a good idea to finish quickly, as the virtual machine really wants to shut down!
There are no guarantees that such hooks will run, as there are ways to forcibly kill a process. But in such a drastic case, shutdown hooks would do little for you in the first place.
Parameters: hook an initialized, unstarted Thread
Throws: IllegalArgumentException if the hook is already registered or run IllegalStateException if the virtual machine is already in the shutdown sequence SecurityException if permission is denied
Since: 1.3
See Also: removeShutdownHook Runtime Runtime
Returns: the number of processors available, at least 1
exec(cmdline, null, null)
. A security check is performed,
checkExec
.
Parameters: cmdline the command to call
Returns: the Process object
Throws: SecurityException if permission is denied IOException if an I/O error occurs NullPointerException if cmdline is null IndexOutOfBoundsException if cmdline is ""
exec(cmdline, env, null)
. A security
check is performed, checkExec
.
Parameters: cmdline the command to call env the environment to use, in the format name=value
Returns: the Process object
Throws: SecurityException if permission is denied IOException if an I/O error occurs NullPointerException if cmdline is null, or env has null entries IndexOutOfBoundsException if cmdline is ""
exec(cmdArray, env, dir)
. A security check is performed,
checkExec
.
Parameters: cmdline the command to call env the environment to use, in the format name=value dir the working directory to use
Returns: the Process object
Throws: SecurityException if permission is denied IOException if an I/O error occurs NullPointerException if cmdline is null, or env has null entries IndexOutOfBoundsException if cmdline is ""
Since: 1.3
exec(cmd, null, null)
. A security check
is performed, checkExec
.
Parameters: cmd the command to call
Returns: the Process object
Throws: SecurityException if permission is denied IOException if an I/O error occurs NullPointerException if cmd is null, or has null entries IndexOutOfBoundsException if cmd is length 0
exec(cmd, env, null)
. A security check is performed,
checkExec
.
Parameters: cmd the command to call env the environment to use, in the format name=value
Returns: the Process object
Throws: SecurityException if permission is denied IOException if an I/O error occurs NullPointerException if cmd is null, or cmd or env has null entries IndexOutOfBoundsException if cmd is length 0
checkExec
.
Parameters: cmd the command to call env the environment to use, in the format name=value dir the working directory to use
Returns: the Process object
Throws: SecurityException if permission is denied IOException if an I/O error occurs NullPointerException if cmd is null, or cmd or env has null entries IndexOutOfBoundsException if cmd is length 0
Since: 1.3
checkExit(status)
.
First, all shutdown hooks are run, in unspecified order, and
concurrently. Next, if finalization on exit has been enabled, all pending
finalizers are run. Finally, the system calls halt
.
If this is run a second time after shutdown has already started, there
are two actions. If shutdown hooks are still executing, it blocks
indefinitely. Otherwise, if the status is nonzero it halts immediately;
if it is zero, it blocks indefinitely. This is typically called by
System.exit
.
Parameters: status the status to exit with
Throws: SecurityException if permission is denied
See Also: addShutdownHook Runtime runFinalization Runtime
Returns: the number of bytes of free memory for more Objects
Deprecated: InputStreamReader
is the preferred way to read
local encodings
Parameters: in the stream to localize
Returns: the localized stream
UNKNOWN: This implementation does not localize, yet.
Deprecated: OutputStreamWriter
is the preferred way to write
local encodings
Parameters: out the stream to localize
Returns: the localized stream
UNKNOWN: This implementation does not localize, yet.
Returns: the current Runtime object
exit
, as it bypasses all shutdown
hooks and initializers. Use caution in calling this! Of course, there is
a security check, checkExit(status)
.
Parameters: status the status to exit with
Throws: SecurityException if permission is denied
Since: 1.3
See Also: Runtime addShutdownHook
checkLink
.
The library is loaded using the class loader associated with the class associated with the invoking method.
Parameters: filename the file to load
Throws: SecurityException if permission is denied UnsatisfiedLinkError if the library is not found
cl.findLibrary(libpath)
is
used to convert the name. If that result was null, or there was no class
loader, this searches each directory of the system property
java.library.path
for a file named
System.mapLibraryName(libname)
. There may be a security
check, of checkLink
.
Note: Besides java.library.path
a VM may chose to search
for native libraries in a path that is specified by the
gnu.classpath.boot.library.path
system property. However
this is for internal usage or development of GNU Classpath only.
A Java application must not load a non-system library by changing
this property otherwise it will break compatibility.
The library is loaded using the class loader associated with the class associated with the invoking method.
Parameters: libname the library to load
Throws: SecurityException if permission is denied UnsatisfiedLinkError if the library is not found
See Also: mapLibraryName findLibrary
Long.MAX_VALUE
if there is no inherent
limit (or if you really do have a 8 exabyte memory!).
Returns: the maximum number of bytes the virtual machine will attempt to allocate
RuntimePermission("shutdownHooks")
.
Parameters: hook the hook to remove
Returns: true if the hook was successfully removed, false if it was not registered in the first place
Throws: IllegalStateException if the virtual machine is already in the shutdown sequence SecurityException if permission is denied
Since: 1.3
See Also: addShutdownHook Runtime Runtime
finalize
method of all objects waiting to be collected.
See Also: Runtime
Deprecated: never rely on finalizers to do a clean, thread-safe, mop-up from your code
Tell the VM to run the finalize() method on every single Object before it exits. Note that the JVM may still exit abnormally and not perform this, so you still don't have a guarantee. And besides that, this is inherently unsafe in multi-threaded code, as it may result in deadlock as multiple threads compete to manipulate objects. This value defaults tofalse
. There is a security check, checkExit(0)
.
Parameters: finalizeOnExit whether to finalize all Objects on exit
Throws: SecurityException if permission is denied
Since: 1.1
Returns: the total number of bytes of memory for Objects
Parameters: on whether to turn instruction tracing on
Parameters: on whether to turn method tracing on