Visit Software Engineering Space for more articles

How to add and read system properties?

Read system properties in program. Also, add system properties programmatically and as JVM parameter.

Code Explanation

java.lang.System class provides methods to read, add and modify system properties. By default many important system properties are available. For example, current user directory, operating system etc. Also, System properties can be set during program (JVM) startup using one or more -Dname=value JVM parameters. JVM parameters can be used to pass application level parameters to the program instead of hard coding those in program. Let's have a glance on System methods.

  1. To read a single property use - System.getProperty("name")
  2. To read a single property by specifying default value use - System.getProperty("name", "default-value"). Default value is returned when requested property not found.
  3. To read all system properties use - System.getProperties(). The returned enumeration can be iterated to access all the properties.
  4. To add or modify a system property use - System.setProperty("name", "value"). If the property name already exists this methods overrides the value. Do not ever modify default system properties.

To pass system properties to program during startup, use following command:

java -Dname=value -Dname1=value1 SystemProperties

In this example first we add few properties to system properties list. Then call showProperties() method to print all system properties to console. Finally, we show how to access individual system property.

21     /**
22      * Main method of the program.
23      * @param args Input arguments.
24      */
25     public static void main(String[] args) {
26         // Set additional system properties
27         System.setProperty("class.name", "org.apex.howto.core.SystemProperties");
28         System.setProperty("class.author", "Mrityunjoy Saha");
29         System.setProperty("class.version", "1.0");
30         // Print system properties
31         new SystemProperties().showProperties();
32         // Get user directory from system  properties
33         String userDirectory = System.getProperty("user.dir");
34         System.out.println("Printing user.dir: " + userDirectory);
35     }
    

This method prints all system properties to console. Once we get hold of java.util.Properties object by calling System.getProperties(), we can print individual properties in two different ways. Call the handy method list(PrintWriter) or list(PrintStream) on properties object. And the other way is to iterate through property keys and print individual properties.

37     /**
38      * Iterates and prints system properties to console.
39      */
40     private void showProperties() {
41         Properties properties = System.getProperties();
42         // Send properties to standard output i.e. console
43         //properties.list(System.out);
44         // Enumerate keys and print key-value pairs to console
45         for (Enumeration e = properties.keys(); e.hasMoreElements();) {
46             String key = (String) e.nextElement();
47             String val = properties.getProperty(key);
48             System.out.println(key + "=" + val);
49         }
50     }
51 }
52 
53 
    

Download

You may download the complete source code from here: