System properties provide a mechanism for getting information about the environment. You can get the value of a system property by passing its name to the System.getProperty(String) method. This method returns the value of the named property as a String, or it returns null if the property is not defined. Since it is common to assume a default value if a property is not specified, there is also a System.getProperty(String, String) method that takes the name of a property and a default String value to return if the property is not defined.
Table 10.1 lists the standard system properties for a Java environment. Many of these properties are guaranteed to be defined in any Java environment. Note, however, that untrusted applets aren't allowed to access many of these properties.
Property Name |
Description |
---|---|
file.encoding |
The character encoding for the default locale ( Java 1.1 only) |
file.encoding.pkg |
The package that contains the converters that handle converting between local encodings and Unicode ( Java 1.1 only) |
file.separator |
The platform-dependent file separator (e.g., "/" on UNIX, "\" for Windows) |
java.class.path |
The value of the CLASSPATH environment variable |
java.class.version |
The version of the Java API |
java.compiler |
The just-in-time compiler to use, if any. The java interpreter provided with the JDK initializes this property from the environment variable JAVA_COMPILER |
java.home |
The directory in which Java is installed |
java.version |
The version of the Java interpreter |
java.vendor |
A vendor-specific string |
java.vendor.url |
A vendor URL |
line.separator |
The platform-dependent line separator (e.g., "\n" on UNIX, "\r\n" for Windows) |
os.name |
The name of the operating system |
os.arch |
The system architecture |
os.version |
The operating system version |
path.separator |
The platform-dependent path separator (e.g., ":" on UNIX, "," for Windows) |
user.dir |
The current working directory when the properties were initialized |
user.home |
The home directory of the current user |
user.language |
The two-letter language code of the default locale ( Java 1.1 only) |
user.name |
The username of the current user |
user.region |
The two-letter country code of the default locale ( Java 1.1 only) |
user.timezone |
The default time zone ( Java 1.1 only) |
The Java API also provides some convenience methods for getting the value of a system property that should be interpreted as a data type other than String:
By default, the font style is plain and the font size is 12 points. If the font name is prefixed with bold-, italic- or bolditalic-, that style is used instead. If the font name is prefixed with a size and a -, that size is used instead. If both style and size are specified, style must come first. For example, passing "italic-14-timesRoman" to getFont() causes it to return a Font object that uses the native font identified by the system property awt.font.timesRoman. That font should be an italic, 14-point, TimesRoman font.
There are two built-in mechanisms for setting system properties. Note that you can use these mechanisms to set the standard system properties or to define specific system properties for your own application.
C:\> java -Dtime.server=tm02
You can programmatically define properties by calling the System.setProperties() method. The Properties object that you pass to System.setProperties() becomes the new source for all system property values.
If a program is running in a browser or other environment that has a SecurityManager installed, it may be denied any access to system properties.