Java System Properties Example

In this example we are going to demonstrate how to use Java System Properties. Java application can read System Properties, which provide information about the local system and configuration. When the java VM starts, its inserts local system properties into a system properties list. You can then use methods defined as part of the System Class to maintain the value of these properties.

1. Information of System Properties:

"file.separator"Character that separates components of a file path. This is “/” on UNIX and “\” on Windows.
"java.class.path"Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
"java.home"Installation directory for Java Runtime Environment (JRE)
"java.vendor"JRE vendor name
"java.vendor.url"JRE vendor URL
"java.version"JRE version number
"line.separator"Sequence used by operating system to separate lines in text files
"os.arch"Operating system architecture
""Operating system name
"os.version"Operating system version
"path.separator"Path separator character used in java.class.path
"user.dir"User working directory
"user.home"User home directory
""User account name

You can use most of the above properties in your applications such as saving some temp or log files in the user directory or getting the user name to display it.

2. Writing System Properties:

To modify the existing set of system properties, use System.setProperties. This method takes a Properties object that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by the Properties object.

3. Reading System Properties:

There are two System class methods used to read system properties: getProperty and getProperties.

3.1. getProperty:

Retrieve the value of the property named in the argument list, there are two different versions of getProperty.

  • Single argument version:

    Returns a string containing the value of the property based on the given property key but it will return null if the property does not exist. For example, to get the value of, use the following statement:

  • Two argument version:

    It 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, the following invocation of getProperty looks up the System property called fake.prop. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: “another value here!”

    System.getProperty("fake.prop", "another value here!");

3.2. getProperties:

The System Class provide this method to access property values, which returns a Properties object. This object contains a complete set of system property definitions.

4. Example:


import java.util.Properties;

public class SystemProperties {
    public static void main(String[] args)
        throws Exception {

        // creates a FileInputStream from file newProp.txt to load it into the new properties object 
        FileInputStream propFile =
            new FileInputStream("newProp.txt");

        // initializes p with the current set of system properties
        Properties p =
            new Properties(System.getProperties());
        // loads additional properties into p from the file newProp.txt

        // set the new system properties

        // display the new system properties

4.2. Explanation:

  • In the above example, SystemProperties creates a Properties object which contains set of current system properties then add a new property from newProp.txt which contains:
    new.prop=new prop value here! Author
  • The following statement initializes the new properties object p, with the current set of system properties, which in the case of this small application, is the set of properties initialized by the runtime system.
    Properties p = new Properties(System.getProperties());
  • Then the application loads additional properties into p from the file newProp.txt.
  • After that the application sets the system properties to p. This has the effect of adding the properties listed in newProp.txt to the set of properties created by the runtime system at startup.

4.3. Notes:

  • An application can create p without any default Properties object, like this:
    Properties p = new Properties();
  • The value of system properties can be overwritten! For example, the newProp.txt contains the following line, the system property will be overwritten: Author

4.4. Output:

-- listing properties -- SE Runtime Environment
new.prop=new prop value here!
java.vm.vendor=Sun Microsystems Inc.
path.separator=: HotSpot(TM) Server VM
sun.os.patch.level=unknown Virtual Machine Specification

java.vm.specification.vendor=Sun Microsystems Inc.
java.library.path=/usr/lib/jvm/java-6-sun- Platform API Specification
java.class.version=50.0 Tiered Compilers
java.specification.version=1.6 Author
java.specification.vendor=Sun Microsystems Inc.
user.language=en mode
java.vendor=Sun Microsystems Inc.

5. Download the Source Code of this example:

This was an example of how to use Java System Properties.

You can download the full source code of this example here : JavaSystemPropertiesExampleCode

Ashraf Sarhan

Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button