Java System.clearProperty() – Examples
In this tutorial, we will learn about the Java System.clearProperty() function, and learn how to use this function to remove a system property, with the help of examples.
clearProperty(String key)
System.clearProperty() removes the system property indicated by the specified key and returns the value for the key, that is removed.
If there is no property specified by the key, clearProperty() returns null.
Syntax
The syntax of clearProperty() function is
clearProperty(String key)
where
Parameter | Description |
---|---|
key | The name of the system property to be removed. |
Returns
The function returns static String.
Example 1 – clearProperty(key)
In this example, we will clear the property identified by the key java.class.path
using clearProperty()
method. We will also print the value returned by this method.
Java Program
public class Example {
public static void main(String args[]) {
String key = "java.class.path";
System.out.println("Before clearProperty() = " + System.getProperty(key));
String returnedValue = System.clearProperty(key);
System.out.println("Returned Value = " + returnedValue);
System.out.println("After clearProperty() = "+System.getProperty(key));
}
}
Output
Before clearProperty() = C:\workspace\eclipse\JavaTutorial\bin;
Returned Value = C:\workspace\eclipse\JavaTutorial\bin;
After clearProperty() = null
Example 2 – clearProperty(key) – Key not present
In this example, we will clear the property identified by the key java.class.xyz
using clearProperty()
method. Since java.class.xyz
is not a valid property, clearProperty()
returns null
.
Java Program
public class Example {
public static void main(String args[]) {
String key = "java.class.xyz";
System.out.println("Before clearProperty() = " + System.getProperty(key));
String returnedValue = System.clearProperty(key);
System.out.println("Returned Value = " + returnedValue);
System.out.println("After clearProperty() = "+System.getProperty(key));
}
}
Output
Before clearProperty() = null
Returned Value = null
After clearProperty() = null
Example 3 – clearProperty(key) – Empty Key
In this example, we will provide an empty key to clearProperty()
method. clearProperty()
should throw java.lang.IllegalArgumentException
.
Java Program
public class Example {
public static void main(String args[]) {
String key = "";
System.clearProperty(key);
}
}
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.clearProperty(Unknown Source)
at Example.main(Example.java:4)
Example 4 – clearProperty(key) – Null Key
In this example, we will provide a null value for key to clearProperty()
method. clearProperty()
should throw java.lang.NullPointerException
.
Java Program
public class Example {
public static void main(String args[]) {
String key = null;
System.clearProperty(key);
}
}
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.clearProperty(Unknown Source)
at Example.main(Example.java:4)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java System.clearProperty() function, and also learnt how to use this function with the help of examples.