java.lang
Class System

java.lang.Object
  extended by java.lang.System

public final class System
extends Object

System represents system-wide resources; things that represent the general environment. As such, all methods are static.

Since:
1.0

Field Summary
static PrintStream err
          The standard output PrintStream.
static InputStream in
          The standard InputStream.
static PrintStream out
          The standard output PrintStream.
 
Method Summary
static void arraycopy(Object src, int srcStart, Object dest, int destStart, int len)
          Copy one array onto another from src[srcStart] ...
static String clearProperty(String key)
          Remove a single system property by name.
static long currentTimeMillis()
          Get the current time, measured in the number of milliseconds from the beginning of Jan. 1, 1970.
static void exit(int status)
          Terminate the Virtual Machine.
static void gc()
          Calls the garbage collector.
static Map<String,String> getenv()
           Returns an unmodifiable view of the system environment variables.
static String getenv(String name)
          Gets the value of an environment variable.
static Properties getProperties()
          Get all the system properties at once.
static String getProperty(String key)
          Get a single system property by name.
static String getProperty(String key, String def)
          Get a single system property by name.
static SecurityManager getSecurityManager()
          Get the current SecurityManager.
static int identityHashCode(Object o)
          Get a hash code computed by the VM for the Object.
static Channel inheritedChannel()
          Returns the inherited channel of the VM.
static void load(String filename)
          Load a code file using its explicit system-dependent filename.
static void loadLibrary(String libname)
          Load a library using its explicit system-dependent filename.
static String mapLibraryName(String libname)
          Convert a library name to its platform-specific variant.
static long nanoTime()
          Get the current time, measured in nanoseconds.
static void runFinalization()
          Runs object finalization on pending objects.
static void runFinalizersOnExit(boolean finalizeOnExit)
          Deprecated. never rely on finalizers to do a clean, thread-safe, mop-up from your code
static void setErr(PrintStream err)
          Set err to a new PrintStream.
static void setIn(InputStream in)
          Set in to a new InputStream.
static void setOut(PrintStream out)
          Set out to a new PrintStream.
static void setProperties(Properties properties)
          Set all the system properties at once.
static String setProperty(String key, String value)
          Set a single system property by name.
static void setSecurityManager(SecurityManager sm)
          Set the current SecurityManager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

public static final InputStream in
The standard InputStream. This is assigned at startup and starts its life perfectly valid. Although it is marked final, you can change it using setIn(InputStream) through some hefty VM magic.

This corresponds to the C stdin and C++ cin variables, which typically input from the keyboard, but may be used to pipe input from other processes or files. That should all be transparent to you, however.


out

public static final PrintStream out
The standard output PrintStream. This is assigned at startup and starts its life perfectly valid. Although it is marked final, you can change it using setOut(PrintStream) through some hefty VM magic.

This corresponds to the C stdout and C++ cout variables, which typically output normal messages to the screen, but may be used to pipe output to other processes or files. That should all be transparent to you, however.


err

public static final PrintStream err
The standard output PrintStream. This is assigned at startup and starts its life perfectly valid. Although it is marked final, you can change it using setErr(PrintStream) through some hefty VM magic.

This corresponds to the C stderr and C++ cerr variables, which typically output error messages to the screen, but may be used to pipe output to other processes or files. That should all be transparent to you, however.

Method Detail

setIn

public static void setIn(InputStream in)
Set in to a new InputStream. This uses some VM magic to change a "final" variable, so naturally there is a security check, RuntimePermission("setIO").

Parameters:
in - the new InputStream
Throws:
SecurityException - if permission is denied
Since:
1.1

setOut

public static void setOut(PrintStream out)
Set out to a new PrintStream. This uses some VM magic to change a "final" variable, so naturally there is a security check, RuntimePermission("setIO").

Parameters:
out - the new PrintStream
Throws:
SecurityException - if permission is denied
Since:
1.1

setErr

public static void setErr(PrintStream err)
Set err to a new PrintStream. This uses some VM magic to change a "final" variable, so naturally there is a security check, RuntimePermission("setIO").

Parameters:
err - the new PrintStream
Throws:
SecurityException - if permission is denied
Since:
1.1

setSecurityManager

public static void setSecurityManager(SecurityManager sm)
Set the current SecurityManager. If a security manager already exists, then RuntimePermission("setSecurityManager") is checked first. Since this permission is denied by the default security manager, setting the security manager is often an irreversible action.

Parameters:
sm - the new SecurityManager
Throws:
SecurityException - if permission is denied

getSecurityManager

public static SecurityManager getSecurityManager()
Get the current SecurityManager. If the SecurityManager has not been set yet, then this method returns null.

Returns:
the current SecurityManager, or null

currentTimeMillis

public static long currentTimeMillis()
Get the current time, measured in the number of milliseconds from the beginning of Jan. 1, 1970. This is gathered from the system clock, with any attendant incorrectness (it may be timezone dependent).

Returns:
the current time
See Also:
Date

nanoTime

public static long nanoTime()
Get the current time, measured in nanoseconds. The result is as precise as possible, and is measured against a fixed epoch. However, unlike currentTimeMillis(), the epoch chosen is arbitrary and may vary by platform, etc.

Since:
1.5

arraycopy

public static void arraycopy(Object src,
                             int srcStart,
                             Object dest,
                             int destStart,
                             int len)
Copy one array onto another from src[srcStart] ... src[srcStart+len-1] to dest[destStart] ... dest[destStart+len-1]. First, the arguments are validated: neither array may be null, they must be of compatible types, and the start and length must fit within both arrays. Then the copying starts, and proceeds through increasing slots. If src and dest are the same array, this will appear to copy the data to a temporary location first. An ArrayStoreException in the middle of copying will leave earlier elements copied, but later elements unchanged.

Parameters:
src - the array to copy elements from
srcStart - the starting position in src
dest - the array to copy elements to
destStart - the starting position in dest
len - the number of elements to copy
Throws:
NullPointerException - if src or dest is null
ArrayStoreException - if src or dest is not an array, if they are not compatible array types, or if an incompatible runtime type is stored in dest
IndexOutOfBoundsException - if len is negative, or if the start or end copy position in either array is out of bounds

identityHashCode

public static int identityHashCode(Object o)
Get a hash code computed by the VM for the Object. This hash code will be the same as Object's hashCode() method. It is usually some convolution of the pointer to the Object internal to the VM. It follows standard hash code rules, in that it will remain the same for a given Object for the lifetime of that Object.

Parameters:
o - the Object to get the hash code for
Returns:
the VM-dependent hash code for this Object
Since:
1.1

getProperties

public static Properties getProperties()
Get all the system properties at once. A security check may be performed, checkPropertiesAccess. Note that a security manager may allow getting a single property, but not the entire group.

The required properties include:

java.version
Java version number
java.vendor
Java vendor specific string
java.vendor.url
Java vendor URL
java.home
Java installation directory
java.vm.specification.version
VM Spec version
java.vm.specification.vendor
VM Spec vendor
java.vm.specification.name
VM Spec name
java.vm.version
VM implementation version
java.vm.vendor
VM implementation vendor
java.vm.name
VM implementation name
java.specification.version
Java Runtime Environment version
java.specification.vendor
Java Runtime Environment vendor
java.specification.name
Java Runtime Environment name
java.class.version
Java class version number
java.class.path
Java classpath
java.library.path
Path for finding Java libraries
java.io.tmpdir
Default temp file path
java.compiler
Name of JIT to use
java.ext.dirs
Java extension path
os.name
Operating System Name
os.arch
Operating System Architecture
os.version
Operating System Version
file.separator
File separator ("/" on Unix)
path.separator
Path separator (":" on Unix)
line.separator
Line separator ("\n" on Unix)
user.name
User account name
user.home
User home directory
user.dir
User's current working directory
In addition, gnu defines several other properties, where ? stands for each character in '0' through '9':
gnu.classpath.home
Path to the classpath libraries.
gnu.classpath.version
Version of the classpath libraries.
gnu.classpath.vm.shortname
Succinct version of the VM name; used for finding property files in file system
gnu.classpath.home.url
Base URL; used for finding property files in file system
gnu.cpu.endian
big or little
gnu.java.io.encoding_scheme_alias.ISO-8859-?
8859_?
gnu.java.io.encoding_scheme_alias.iso-8859-?
8859_?
gnu.java.io.encoding_scheme_alias.iso8859_?
8859_?
gnu.java.io.encoding_scheme_alias.iso-latin-_?
8859_?
gnu.java.io.encoding_scheme_alias.latin?
8859_?
gnu.java.io.encoding_scheme_alias.UTF-8
UTF8
gnu.java.io.encoding_scheme_alias.utf-8
UTF8
gnu.java.util.zoneinfo.dir
Root of zoneinfo tree

Returns:
the system properties, will never be null
Throws:
SecurityException - if permission is denied

setProperties

public static void setProperties(Properties properties)
Set all the system properties at once. A security check may be performed, checkPropertiesAccess. Note that a security manager may allow setting a single property, but not the entire group. An argument of null resets the properties to the startup default.

Parameters:
properties - the new set of system properties
Throws:
SecurityException - if permission is denied

getProperty

public static String getProperty(String key)
Get a single system property by name. A security check may be performed, checkPropertyAccess(key).

Parameters:
key - the name of the system property to get
Returns:
the property, or null if not found
Throws:
SecurityException - if permission is denied
NullPointerException - if key is null
IllegalArgumentException - if key is ""

getProperty

public static String getProperty(String key,
                                 String def)
Get a single system property by name. A security check may be performed, checkPropertyAccess(key).

Parameters:
key - the name of the system property to get
def - the default
Returns:
the property, or def if not found
Throws:
SecurityException - if permission is denied
NullPointerException - if key is null
IllegalArgumentException - if key is ""

setProperty

public static String setProperty(String key,
                                 String value)
Set a single system property by name. A security check may be performed, checkPropertyAccess(key, "write").

Parameters:
key - the name of the system property to set
value - the new value
Returns:
the previous value, or null
Throws:
SecurityException - if permission is denied
NullPointerException - if key is null
IllegalArgumentException - if key is ""
Since:
1.2

clearProperty

public static String clearProperty(String key)
Remove a single system property by name. A security check may be performed, checkPropertyAccess(key, "write").

Parameters:
key - the name of the system property to remove
Returns:
the previous value, or null
Throws:
SecurityException - if permission is denied
NullPointerException - if key is null
IllegalArgumentException - if key is ""
Since:
1.5

getenv

public static String getenv(String name)
Gets the value of an environment variable.

Parameters:
name - the name of the environment variable
Returns:
the string value of the variable or null when the environment variable is not defined.
Throws:
NullPointerException
SecurityException - if permission is denied
Since:
1.5

getenv

public static Map<String,String> getenv()

Returns an unmodifiable view of the system environment variables. If the underlying system does not support environment variables, an empty map is returned.

The returned map is read-only and does not accept queries using null keys or values, or those of a type other than String. Attempts to modify the map will throw an UnsupportedOperationException, while attempts to pass in a null value will throw a NullPointerException. Types other than String throw a ClassCastException.

As the returned map is generated using data from the underlying platform, it may not comply with the equals() and hashCode() contracts. It is also likely that the keys of this map will be case-sensitive.

Use of this method may require a security check for the RuntimePermission "getenv.*".

Returns:
a map of the system environment variables.
Throws:
SecurityException - if the checkPermission method of an installed security manager prevents access to the system environment variables.
Since:
1.5

exit

public static void exit(int status)
Terminate the Virtual Machine. This just calls Runtime.getRuntime().exit(status), and never returns. Obviously, a security check is in order, checkExit.

Parameters:
status - the exit status; by convention non-zero is abnormal
Throws:
SecurityException - if permission is denied
See Also:
Runtime.exit(int)

gc

public static void gc()
Calls the garbage collector. This is only a hint, and it is up to the implementation what this hint suggests, but it usually causes a best-effort attempt to reclaim unused memory from discarded objects. This calls Runtime.getRuntime().gc().

See Also:
Runtime.gc()

runFinalization

public static void runFinalization()
Runs object finalization on pending objects. This is only a hint, and it is up to the implementation what this hint suggests, but it usually causes a best-effort attempt to run finalizers on all objects ready to be reclaimed. This calls Runtime.getRuntime().runFinalization().

See Also:
Runtime.runFinalization()

runFinalizersOnExit

public static void runFinalizersOnExit(boolean finalizeOnExit)
Deprecated. never rely on finalizers to do a clean, thread-safe, mop-up from your code

Tell the Runtime whether to run finalization before exiting the JVM. This is inherently unsafe in multi-threaded applications, since it can force initialization on objects which are still in use by live threads, leading to deadlock; therefore this is disabled by default. There may be a security check, checkExit(0). This calls Runtime.getRuntime().runFinalizersOnExit().

Parameters:
finalizeOnExit - whether to run finalizers on exit
Throws:
SecurityException - if permission is denied
Since:
1.1
See Also:
Runtime#runFinalizersOnExit()

load

public static void load(String filename)
Load a code file using its explicit system-dependent filename. A security check may be performed, checkLink. This just calls Runtime.getRuntime().load(filename).

The library is loaded using the class loader associated with the class associated with the invoking method.

Parameters:
filename - the code file to load
Throws:
SecurityException - if permission is denied
UnsatisfiedLinkError - if the file cannot be loaded
See Also:
Runtime.load(String)

loadLibrary

public static void loadLibrary(String libname)
Load a library using its explicit system-dependent filename. A security check may be performed, checkLink. This just calls Runtime.getRuntime().load(filename).

The library is loaded using the class loader associated with the class associated with the invoking method.

Parameters:
libname - the library file to load
Throws:
SecurityException - if permission is denied
UnsatisfiedLinkError - if the file cannot be loaded
See Also:
Runtime.load(String)

mapLibraryName

public static String mapLibraryName(String libname)
Convert a library name to its platform-specific variant.

Parameters:
libname - the library name, as used in loadLibrary
Returns:
the platform-specific mangling of the name
Since:
1.2

inheritedChannel

public static Channel inheritedChannel()
                                throws IOException
Returns the inherited channel of the VM. This wraps the inheritedChannel() call of the system's default SelectorProvider.

Returns:
the inherited channel of the VM
Throws:
IOException - If an I/O error occurs
SecurityException - If an installed security manager denies access to RuntimePermission("inheritedChannel")
Since:
1.5