How do I clear system property?

System.clearProperty(String key) method enables you to remove a system property. The key must not be an empty string or a null value because it will cause the method to throw an IllegalArgumentException or a NullPointerException.

It will also check if a SecurityManager exists and if you don’t have a write permission to the system property a SecurityException is going to be thrown.

package org.kodejava.lang;

public class ClearProperty {
    public static void main(String[] args) {
        String key = "user.dir";
        System.out.println(key + " = " + System.getProperty(key));

        // The System.clearProperty() method available since Java 1.5
        System.clearProperty(key);
        System.out.println(key + " = " + System.getProperty(key));
    }
}

The code snippet above give us the following output:

user.dir = F:\Wayan\Kodejava\kodejava-example
user.dir = null
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.