A Guide to Java System Properties

System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System.setProperty(String key, String value) or via the various System.getProperties().load() methods. To get a specific system property you can use the System.getProperty(String key) or System.getProperty(String key, String def).

Each java system property is a key-value (String-String) pair.

Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows, etc, and, unlike properties, may not be set at runtime. To get a specific environment variable you can use System.getenv(String name).

Reading System Properties

The System class has two methods used to read 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 take a single argument, a property key 

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.

The other version of getProperty 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 subliminal.message. This is not valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Buy StayPuft Marshmallows!"

System.getProperty("user.message", "Buy StayPuft Marshmallows!");

The last method provided by the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.

Reading System Properties

The System class has two methods used to read 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 take a single argument, a property key, 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.

The other version of getProperty 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 subliminal.message. This is not valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Buy StayPuft Marshmallows!"

System.getProperty("user.message", "Buy StayPuft Marshmallows!");

The last method provided by the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.

Java program to get the list of all system properties

import java.util.Properties; public class PrintSystemProperties { public static void main(String[] a) { Properties pros = System.getProperties(); System.out.println("the system properties are : "); pros.list(System.out); } }

Important Points about Java System Properties :

  • java.lang.System.getProperty(String key): fetches only those properties – values that you will specify using the key(associated with that particular value that you want).
  • java.lang.System.getProperty(String key, String def) : helps you to create your own key-value sets that you want.
  • java.lang.System.getProperties(): fetches all the properties – values that the JVM on your System gets from the Operating System.

In this article, we have seen Java System Properties with Examples.