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 Applet Access ------------------- ------------------------------ ------------- "file.separator" File separator (e.g., "/") yes "java.class.path" Java classpath no "java.class.version" Java class version number yes "java.home" Java installation directory no "java.vendor" Java vendor-specific string yes "java.vendor.url" Java vendor URL yes "java.version" Java version number yes "line.separator" Line separator yes "os.arch" Operating system architecture yes "os.name" Operating system name yes "path.separator" Path separator (e.g., ":") yes "user.dir" User's current working directory no "user.home" User home directory no "user.name" User account name noYour 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.
Security consideration: Applets can access some, but not all system properties. For a complete list of the system properties that can 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()
. Both retrieve the value of the property named in the argument list. The simpler 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 Properties object 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 usessubliminal.message=Buy Java Now!System.setProperties()
to install the new Properties objects as the current set of system properties.Notice how the example program creates 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); // display new properties } catch (java.io.FileNotFoundException e) { System.err.println("Can't find myProperties.txt."); } catch (java.io.IOException e) { System.err.println("I/O failed."); } } }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 sets 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. Notice that you could createp
without any default Properties object like this:If you do this then your application won't have access to the system properties.Properties p = new Properties();Also note that the value of system properties can be overwritten! For example, if
myProperties.txt
contains the following line, thejava.vendor
system property wil be overwritten:In general, you should be careful not to overwrite system properties.java.vendor=Acme Software CompanyThe
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 |