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.

Properties sysProps = System.getProperties();
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()


Anonymous said...

thanks ;)

Anonymous said...

Is there any way to do this from the command line?

javahowto said...

Short answer: no.

From commandline, all you can do is change the JVM options (-D option for system properties). If you don't need a system property, then don't specify it.

You can also override the built-in system properties (such as os.name, user.name, line.separator, etc) by specifying them in command line.

For example:

public class Test {
public static void main(String args[]) {
System.out.println("[" + System.getProperty("user.name") + "]");
System.out.println("[" + System.getProperty("os.name") + "]");

$ java Test
[Mac OS X]

$ java -Duser.name=tester -Dos.name=Windows Test


I don't see any need to override built-in sysprops. This is just to show what can be done.

casey said...

To get a list of all java system properties:

import java.util.Properties;
import java.util.Enumeration;

public class SysProp {
public static void main(String[] args) {
Properties sysprops = System .getProperties();
Enumeration e = sysprops.propertyNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String value = sysprops.getProperty(key);
System.out.println(key + "=" + value);

(pasted from here)

Blaine said...

To list all Java properties without coding anything, or to display specified Java property, is to use SqlTool.

Download jar file using the Download "jar" link at http://search.maven.org/#search|ga|1|g%3A%22org.hsqldb%22%20AND%20a%3A%22sqltool%22 ,

You can then dump all Java system properties with: java -jar path/to/sqltool-VERSION.jar --sql "*listsysprops"

To list a specific system property value, just add it after 'listsysprops', like: java -jar path/to/sqltool-VERSION.jar --sql "*listsysprops user.home"

Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India