![]() ![]() ![]() ![]() |
Using System Resources |
The System class maintains a set of properties--key/value pairs--that define traits or attributes of the current working environment. When the runtime system first starts up, the system properties are initialized to contain information about the runtime environment. including information about the current user, the current version of the Java runtime, and even the character used to separate components of a filename.Here is a complete list of the system properties you get when the runtime system first starts up and what they mean:
Key Meaning ------------------- ------------------------------ "file.separator" File separator (e.g., "/") "java.class.path" Java classpath "java.class.version" Java class version number "java.home" Java installation directory "java.vendor" Java vendor-specific string "java.vendor.url" Java vendor URL "java.version" Java version number "line.separator" Line separator "os.arch" Operating system architecture "os.name" Operating system name "path.separator" Path separator (e.g., ":") "user.dir" User's current working directory "user.home" User home directory "user.name" User account nameYour Java programs can read or write system properties through several methods in the System class. You can use a key to look up one property in the properties list, or you can get the whole set of properties all at once. You can also change the set of system properties completely.
Note: Applets can access some, but not all system properties. For a complete list of the system properties that can be and cannot be used by applets refer to Reading System Properties. Applets cannot write system properties.
Reading System Properties
The System class has two methods that you can use to read the system properties:getProperty()
andgetProperties
.The System class has two different versions of
getProperty()
which retrieve the value of the property named in the argument list. The most simple of the twogetProperty()
methods takes a single argument: the key for the property you want to search for. For example, to get the value ofpath.separator
, use the following statement:TheSystem.getProperty("path.separator");getProperty()
method returns a string containing the value of the property. If the property does not exist, this version ofgetProperty()
returns null.Which brings us to the next version of
getProperty()
method. This version requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, this call togetProperty()
looks up the System property calledsubliminal.message
. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument:Buy Java Now!
.You would use this version ofSystem.getProperty("subliminal.message", "Buy Java Now!");getProperty()
if you didn't want to risk a NullPointerException, or if you really wanted to provide a default value for a property that didn't have value or that couldn't be found.The last method provided by the System class to access properties values is the
getProperties()
method which returns a Propertiesobject that contains the complete set of system property key/value pairs. You can use the various Properties class methods to query the Properties objects for specific values or to list the entire set of properties. For information about the Properties class, see Setting Up and Using Properties
.
Writing System Properties
You can modify the existing set of system properties using System'ssetProperties()
method. This method takes a Properties object that has been initialized to contain the key/value pairs for the properties that you want to set. Thus this method replaces the entire set of system properties with the new set represented by the Properties object.Here's a small example program that creates a Properties object and initializes it from this file: myProperties.txt. The example program then uses
System.setProperties()
to install the new Properties objects as the current set of system properties.Notice how the example program created the Properties object,import java.io.FileInputStream; import java.util.Properties; class PropertiesTest { public static void main(String args[]) { try { // set up new properties object from file "myProperties.txt" FileInputStream propFile = new FileInputStream("myProperties.txt"); Properties p = new Properties(System.getProperties()); p.load(propFile); // set the system properties System.setProperties(p); System.getProperties().list(System.out); } catch (java.io.FileNotFoundException e) { ; } catch (java.io.IOException e) { ; } } }p
, which is used as the argument tosetProperties()
:This statement initializes the new properties object,Properties p = new Properties(System.getProperties());p
with the current set of system properties, which in the case of this small program, is the set of properties initialized by the runtime system. Then the program loads additional properties intop
from the filemyProperties.txt
and set the system properties top
. This has the effect of adding the properties listed inmyProperties.txt
to the set of properties created by the runtime system at startup. The test program took care to keep the properties initialized for it by the runtime system. Your applications will likely want to do the same.The
setProperties()
method changes the set of system properties for the current running application. These changes are not persistent, that is, changing the system properties within an application will not effect future invocations of the Java interpreter for this or any other application--the runtime system re-initializes the system properties each time its starts up. If you want your changes to the system properties to be persistent, then your application must write the values to some file before exiting and read them in again upon startup.
Note: Previous versions of the System class supported a method calledgetenv()
which retrieved the value of an environment variable. This method was Unix specific and has been made obsolete by the more versatile properties mechanism. Your Java programs should not be usinggetenv()
anymore.
See also
java.util.Properties
![]() ![]() ![]() ![]() |
Using System Resources |