Provide Best Programming Tutorials

Java System Properties

System Properties

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.

Let’s take a look at an example:

In IDE “VM Options” section i write a statement -Dfavorite.day=Sunday

In program below i display these properties on the console.

This means we transport these parameters to the Runntime environment of this program.

Here is a complete list of the system properties you get when the runtime system first starts up and what they mean:

Your 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.

Reading System Properties

The System class has two methods that you can use to read the system properties: getProperty and getProperties.

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 two getProperty methods takes a single argument: the key for the property you want to search for. For example, to get the value of path.separator, use the following statement:

System.getProperty("path.separator");

The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty 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 to getProperty looks up the System property called subliminal.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!

System.getProperty("subliminal.message", "Buy Java Now!");

You should use this version of getProperty if you don’t want to risk a NullPointerException, or if you really want to provide a default value for a property that doesn’t have value or that cannot be found.

Writing System Properties

You can modify the existing set of system properties using System setProperties method. This method takes a Propertiesobject 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 Propertiesobject.

subliminal.message=Buy Java Now!

The example program then uses

System.setProperties to install the new Properties

objects as the current set of system properties.

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.");
        }
    }
}

Note how the example program creates the Propertiesobject, p, which is used as the argument to setProperties:

Properties p = new Properties(System.getProperties());

This statement initializes the new properties object,

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 into p from the file

myProperties.txt and sets the system properties to

p. This has the effect of adding the properties listed in myProperties.txtto the set of properties created by the runtime system at startup. Note that you can create pwithout any default Properties

object like this:Properties p = new Properties();

If you do this then your application won’t have access to the system properties.

Also note that the value of system properties can be overwritten! For example, if myProperties.txt contains the following line, the java.vendor system property wil be overwritten:

java.vendor=Acme Software Company

In general, you should be careful not to overwrite system properties.

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.

Leave a Reply

Close Menu