Java System.setProperties() – Examples

In this tutorial, we will learn about the Java System.setProperties() function, and learn how to use this function to set specific properties for System properties, with the help of examples.

setProperties(Properties props)

System.setProperties() sets the system properties to the Properties argument.

If Properties object is null, then System.setProperties() does nothing, and the System properties would be default to the pre-existing properties.

ADVERTISEMENT

Syntax

The syntax of setProperties() function is

setProperties(Properties props)

where

ParameterDescription
propsThe new set of properties for System.

Returns

The function returns void.

Example 1 – setProperties()

In this example, we will create and initialize a Properties object properties, and pass this as argument to setProperties() method. To check if the properties are set, we will print out all the System properties.

Java Program

import java.util.Properties;

public class Example {
	public static void main(String[] args) {
		Properties properties = new Properties();  
		properties.put("x", "y");
		properties.put("a", "b");
		properties.put("m", "n");

		System.setProperties(properties);  

		System.out.println(System.getProperties());  
	}
}

Output

{a=b, x=y, m=n}

Example 2 – setProperties()

In this example, we will create a Properties object properties and initialize it with null, and pass this as argument to setProperties() method. Since properties is null, getProperties() does nothing, and the pre-exiting value for System properties remain the same.

Java Program

public class Example {
	public static void main(String[] args) {
		System.setProperties(null);
		System.out.println(System.getProperties());  
	}
}

Output

{sun.desktop=windows, awt.toolkit=sun.awt.windows.WToolkit, java.specification.version=10,..}

Conclusion

In this Java Tutorial, we have learnt the syntax of Java System.setProperties() function, and also learnt how to use this function with the help of examples.