How to Remove a System Property

I thought I could remove a system property by setting its value to null, but it turns out that System.setProperty(key, val) will throw NullPointerException when either key or value is null. Here are 2 ways to remove or clear a system property:

1. clearProperty(String key) introduced in JDK 1.5.
System.clearProperty("key");
2.
Properties sysProps = System.getProperties();
sysProps.remove("key");
When java security manager is enabled, Both methods need specific permissions to succeed. Approach 1 requires PropertyPermission "key", "read,write". Approach 2 requires a much wider permission: PropertyPermission "*", "read,write", because you are free to modify/remove all system properties with the returned object from System.getProperties()

Followers

Pageviews Last 7 Days