FileDocCategorySizeDatePackage
System.javaAPI DocAndroid 1.5 API25212Wed May 06 22:41:04 BST 2009java.lang

System

public final class System extends Object
Provides access to system-related information and resources including standard input and output. Enables clients to dynamically load native libraries. All methods of this class are accessed in a static way and the class itself can not be instantiated.
see
Runtime
since
Android 1.0

Fields Summary
public static final InputStream
in
Default input stream.
public static final PrintStream
out
Default output stream.
public static final PrintStream
err
Default error output stream.
private static Properties
systemProperties
The System Properties table.
private static SecurityManager
securityManager
The System default SecurityManager.
Constructors Summary
private System()
Prevents this class from being instantiated.

    
Methods Summary
public static native voidarraycopy(java.lang.Object src, int srcPos, java.lang.Object dest, int destPos, int length)
Copies the number of {@code length} elements of the Array {@code src} starting at the offset {@code srcPos} into the Array {@code dest} at the position {@code destPos}.

param
src the source array to copy the content.
param
srcPos the starting index of the content in {@code src}.
param
dest the destination array to copy the data into.
param
destPos the starting index for the copied content in {@code dest}.
param
length the number of elements of the {@code array1} content they have to be copied.
since
Android 1.0

public static java.lang.StringclearProperty(java.lang.String key)
Removes a specific system property.

param
key the name of the system property to be removed.
return
the property value or {@code null} if the property didn't exist.
throws
NullPointerException if the argument {@code key} is {@code null}.
throws
IllegalArgumentException if the argument {@code key} is empty.
throws
SecurityException if a security manager exists and write access to the specified property is not allowed.
since
Android 1.0

        if (key == null) {
            throw new NullPointerException();
        }
        if (key.length() == 0) {
            throw new IllegalArgumentException();
        }

        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPermission(new PropertyPermission(key, "write"));
        }
        return (String)internalGetProperties().remove(key);
    
public static native longcurrentTimeMillis()
Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC. This method shouldn't be used for measuring timeouts or other elapsed time measurements, as changing the system time can affect the results.

return
the local system time in milliseconds.
since
Android 1.0

public static voidexit(int code)
Causes the virtual machine to stop running and the program to exit. If {@link #runFinalizersOnExit(boolean)} has been previously invoked with a {@code true} argument, then all all objects will be properly garbage-collected and finalized first.

param
code the return code.
throws
SecurityException if the running thread has not enough permission to exit the virtual machine.
see
SecurityManager#checkExit
since
Android 1.0

        Runtime.getRuntime().exit(code);
    
public static voidgc()
Indicates to the virtual machine that it would be a good time to run the garbage collector. Note that this is a hint only. There is no guarantee that the garbage collector will actually be run.

since
Android 1.0

        Runtime.getRuntime().gc();
    
private static native java.lang.StringgetEnvByIndex(int index)

private static native java.lang.StringgetEnvByName(java.lang.String name)

public static java.util.PropertiesgetProperties()
Returns the system properties. Note that this is not a copy, so that changes made to the returned Properties object will be reflected in subsequent calls to getProperty and getProperties.

return
the system properties.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPropertiesAccess()} method does not allow the operation.
since
Android 1.0

        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPropertiesAccess();
        }

        return internalGetProperties();
    
public static java.lang.StringgetProperty(java.lang.String prop)
Returns the value of a particular system property or {@code null} if no such property exists.

The properties currently provided by the virtual machine are:

java.vendor.url
java.class.path
user.home
java.class.version
os.version
java.vendor
user.dir
user.timezone
path.separator
os.name
os.arch
line.separator
file.separator
user.name
java.version
java.home

param
prop the name of the system property to look up.
return
the value of the specified system property or {@code null} if the property doesn't exist.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPropertyAccess()} method does not allow the operation.
since
Android 1.0

        return getProperty(prop, null);
    
public static java.lang.StringgetProperty(java.lang.String prop, java.lang.String defaultValue)
Returns the value of a particular system property. The {@code defaultValue} will be returned if no such property has been found.

param
prop the name of the system property to look up.
param
defaultValue the return value if the system property with the given name does not exist.
return
the value of the specified system property or the {@code defaultValue} if the property does not exist.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPropertyAccess()} method does not allow the operation.
since
Android 1.0

        if (prop.length() == 0) {
            throw new IllegalArgumentException();
        }
        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPropertyAccess(prop);
        }

        return internalGetProperties().getProperty(prop, defaultValue);
    
public static java.lang.SecurityManagergetSecurityManager()
Returns the active security manager.

return
the system security manager object.
since
Android 1.0

        return securityManager;
    
public static java.lang.Stringgetenv(java.lang.String name)
Returns the value of the environment variable with the given name {@code var}.

param
name the name of the environment variable.
return
the value of the specified environment variable or {@code null} if no variable exists with the given name.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPermission()} method does not allow the querying of single environment variables.
since
Android 1.0

        if (name == null) {
            throw new NullPointerException();
        }
        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPermission(new RuntimePermission("getenv." + name));
        }

        return getEnvByName(name);
    
public static java.util.Mapgetenv()
Returns an unmodifiable map of all available environment variables.

return
the map representing all environment variables.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPermission()} method does not allow the querying of all environment variables.
since
Android 1.0

        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPermission(new RuntimePermission("getenv.*"));
        }

        Map<String, String> map = new HashMap<String, String>();

        int index = 0;
        String entry = getEnvByIndex(index++);
        while (entry != null) {
            int pos = entry.indexOf('=");
            if (pos != -1) {
                map.put(entry.substring(0, pos), entry.substring(pos + 1));
            }

            entry = getEnvByIndex(index++);
        }

        return new SystemEnvironment(map);
    
public static native intidentityHashCode(java.lang.Object anObject)
Returns an integer hash code for the parameter. The hash code returned is the same one that would be returned by the method {@code java.lang.Object.hashCode()}, whether or not the object's class has overridden hashCode(). The hash code for {@code null} is {@code 0}.

param
anObject the object to calculate the hash code.
return
the hash code for the given object.
see
java.lang.Object#hashCode
since
Android 1.0

public static java.nio.channels.ChannelinheritedChannel()
Returns the inherited channel from the creator of the current virtual machine.

return
the inherited {@link Channel} or {@code null} if none exists.
throws
IOException if an I/O error occurred.
see
SelectorProvider
see
SelectorProvider#inheritedChannel()
since
Android 1.0

        return SelectorProvider.provider().inheritedChannel();
    
static java.util.PropertiesinternalGetProperties()
Returns the system properties without any security checks. This is used for access from within java.lang.

return
the system properties

        if (System.systemProperties == null) {
            SystemProperties props = new SystemProperties();
            props.preInit();
            props.postInit();
            System.systemProperties = props;
        }

        return systemProperties;
    
public static voidload(java.lang.String pathName)
Loads the specified file as a dynamic library.

param
pathName the path of the file to be loaded.
throws
SecurityException if the library was not allowed to be loaded.
since
Android 1.0

        SecurityManager smngr = System.getSecurityManager();
        if (smngr != null) {
            smngr.checkLink(pathName);
        }
        Runtime.getRuntime().load(pathName, VMStack.getCallingClassLoader());
    
public static voidloadLibrary(java.lang.String libName)
Loads and links the shared library with the given name {@code libName}. The file will be searched in the default directory for shared libraries of the local system.

param
libName the name of the library to load.
throws
UnsatisfiedLinkError if the library could not be loaded.
throws
SecurityException if the library was not allowed to be loaded.
since
Android 1.0

        SecurityManager smngr = System.getSecurityManager();
        if (smngr != null) {
            smngr.checkLink(libName);
        }
        Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
    
public static native java.lang.StringmapLibraryName(java.lang.String userLibName)
Returns the platform specific file name format for the shared library named by the argument.

param
userLibName the name of the library to look up.
return
the platform specific filename for the library.
since
Android 1.0

public static native longnanoTime()
Returns the current timestamp of the most precise timer available on the local system. This timestamp can only be used to measure an elapsed period by comparing it against another timestamp. It cannot be used as a very exact system time expression.

return
the current timestamp in nanoseconds.
since
Android 1.0

public static voidrunFinalization()
Provides a hint to the virtual machine that it would be useful to attempt to perform any outstanding object finalizations.

since
Android 1.0

        Runtime.getRuntime().runFinalization();
    
public static voidrunFinalizersOnExit(boolean flag)
Ensures that, when the virtual machine is about to exit, all objects are finalized. Note that all finalization which occurs when the system is exiting is performed after all running threads have been terminated.

param
flag the flag determines if finalization on exit is enabled.
deprecated
this method is unsafe.
since
Android 1.0

        Runtime.runFinalizersOnExit(flag);
    
public static voidsetErr(java.io.PrintStream newErr)
Sets the standard error output stream to the given user defined output stream.

param
newErr the user defined output stream to set as the standard error output stream.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPermission()} method does not allow the change of the stream.
since
Android 1.0

        SecurityManager secMgr = System.getSecurityManager();
        if(secMgr != null) {
            secMgr.checkPermission(RuntimePermission.permissionToSetIO);
        }
        setFieldImpl("err", "Ljava/io/PrintStream;", newErr);
    
private static native voidsetFieldImpl(java.lang.String fieldName, java.lang.String signature, java.lang.Object stream)
Sets the value of the named static field in the receiver to the passed in argument.

param
fieldName the name of the field to set, one of in, out, or err
param
stream the new value of the field

public static voidsetIn(java.io.InputStream newIn)
Sets the standard input stream to the given user defined input stream.

param
newIn the user defined input stream to set as the standard input stream.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPermission()} method does not allow the change of the stream.
since
Android 1.0

        /*
         * Set up standard in, out, and err. TODO err and out are
         * String.ConsolePrintStream. All three are buffered in Harmony. Check
         * and possibly change this later.
         */
        err = new PrintStream(new FileOutputStream(FileDescriptor.err));
        out = new PrintStream(new FileOutputStream(FileDescriptor.out));
        in = new FileInputStream(FileDescriptor.in);
    
        SecurityManager secMgr = System.getSecurityManager();
        if(secMgr != null) {
            secMgr.checkPermission(RuntimePermission.permissionToSetIO);
        }
        setFieldImpl("in", "Ljava/io/InputStream;", newIn);
    
public static voidsetOut(java.io.PrintStream newOut)
Sets the standard output stream to the given user defined output stream.

param
newOut the user defined output stream to set as the standard output stream.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPermission()} method does not allow the change of the stream.
since
Android 1.0

        SecurityManager secMgr = System.getSecurityManager();
        if(secMgr != null) {
            secMgr.checkPermission(RuntimePermission.permissionToSetIO);
        }
        setFieldImpl("out", "Ljava/io/PrintStream;", newOut);
    
public static voidsetProperties(java.util.Properties p)
Sets all system properties.

param
p the new system property.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkPropertiesAccess()} method does not allow the operation.
since
Android 1.0

        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPropertiesAccess();
        }

        systemProperties = p;
    
public static java.lang.StringsetProperty(java.lang.String prop, java.lang.String value)
Sets the value of a particular system property.

param
prop the name of the system property to be changed.
param
value the value to associate with the given property {@code prop}.
return
the old value of the property or {@code null} if the property didn't exist.
throws
SecurityException if a security manager exists and write access to the specified property is not allowed.
since
Android 1.0

        if (prop.length() == 0) {
            throw new IllegalArgumentException();
        }
        SecurityManager secMgr = System.getSecurityManager();
        if (secMgr != null) {
            secMgr.checkPermission(new PropertyPermission(prop, "write"));
        }
        return (String)internalGetProperties().setProperty(prop, value);
    
public static voidsetSecurityManager(java.lang.SecurityManager sm)
Sets the active security manager. Note that once the security manager has been set, it can not be changed. Attempts to do that will cause a security exception.

param
sm the new security manager.
throws
SecurityException if the security manager has already been set and if its checkPermission method does not allow to redefine the security manager.
since
Android 1.0

        if (securityManager != null) {
            securityManager.checkPermission(new java.lang.RuntimePermission("setSecurityManager"));
        }

        if (sm != null) {
            // before the new manager assumed office, make a pass through
            // the common operations and let it load needed classes (if any),
            // to avoid infinite recursion later on
            try {
                sm.checkPermission(new SecurityPermission("getProperty.package.access"));
            } catch (Exception ignore) {
            }
            try {
                sm.checkPackageAccess("java.lang");
            } catch (Exception ignore) {
            }
        }

        securityManager = sm;