Java SystemsetProperty Examples

In this tutorial, we will learn about the Java System.setProperty() function, and learn how to use this function to set a specific property (key-value) in System properties, with the help of examples.

setPropertyString key, String value

System.setProperty() sets the system property indicated by the specified key and returns the value.

Syntax

The syntax of setProperty() function is

setProperty(String key, String value)

where

Parameter Description
key The key is the name (key) of the property.
value The value of the property corresponding to the key in System properties.

Returns

The function returns String.

Example 1 setProperty

In this example, we will set a property with key "x" and value "y" using setProperty() method. After setting the property, we will print all the System properties, if our key-value pair is this set.

Java Program

public class Example {  
	public static void main(String[] args) {
		String key = "x";
		String value = "y";
		System.setProperty(key, value);
		System.out.println(System.getProperties());
	}  
}

Output

{sun.desktop=windows, awt.toolkit=sun.awt.windows.WToolkit, java.specification.version=10, file.encoding.pkg=sun.io, sun.cpu.isalist=amd64, sun.jnu.encoding=Cp1252, x=y, sun.io.unicode.encoding=UnicodeLittle, java.class.version=54.0}

x=y is present in the output.

Example 2 setProperty Return Value

In this example, we will set a property with key "x" and value "y" using setProperty() method. setProperty() returns the value after setting the specified property.

Java Program

public class Example {  
	public static void main(String[] args) {
		String key = "x";
		String value = "y";
		System.setProperty(key, value);
		String returnValue = System.getProperty("x");
		System.out.println(returnValue); 
	}  
}

Output

y

Example 3 setProperty Null Value

In this example, we will set a property with key "x" and value null using setProperty() method. Since, value is null, setProperty() throws java.lang.NullPointerException.

Java Program

public class Example {  
	public static void main(String[] args) {
		String key = "x";
		String value = null;
		System.setProperty(key, value);
	}  
}

Output

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.concurrent.ConcurrentHashMap.putVal(Unknown Source)
	at java.base/java.util.concurrent.ConcurrentHashMap.put(Unknown Source)
	at java.base/java.util.Properties.put(Unknown Source)
	at java.base/java.util.Properties.setProperty(Unknown Source)
	at java.base/java.lang.System.setProperty(Unknown Source)
	at Example.main(Example.java:5)

Example 4 setProperty Null Key

In this example, we will set a property with key null and value "y" using setProperty() method. Since, key is null, setProperty() throws java.lang.NullPointerException.

Java Program

public class Example {  
	public static void main(String[] args) {
		String key = null;
		String value = "y";
		System.setProperty(key, value);
	}  
}

Output

Exception in thread "main" java.lang.NullPointerException: key can't be null
	at java.base/java.lang.System.checkKey(Unknown Source)
	at java.base/java.lang.System.setProperty(Unknown Source)
	at Example.main(Example.java:5)

Example 5 setProperty Empty Key

In this example, we will set a property with key "" and value "y" using setProperty() method. Since, key is empty String, setProperty() throws java.lang.IllegalArgumentException.

Java Program

public class Example {  
	public static void main(String[] args) {
		String key = "";
		String value = "y";
		System.setProperty(key, value);
	}  
}

Output

Exception in thread "main" java.lang.IllegalArgumentException: key can't be empty
	at java.base/java.lang.System.checkKey(Unknown Source)
	at java.base/java.lang.System.setProperty(Unknown Source)
	at Example.main(Example.java:5)

Conclusion

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