JDK-4463345 : System.setProperty(key, value) does not accept null value
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 2001-05-25
  • Updated: 2017-05-16
  • Resolved: 2003-05-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Description
If value is null, System.setProperty(key, value) will throw
NullPointerException. This behavior is not on the spec and makes it
difficult to remove a system property.

The workaround is to use System.setProperties(). However, if system does not
give permissions to read/write all the system properties, this workaround will
fail and there is no way to remove this single property.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b07
14-06-2004

SUGGESTED FIX /** * Removes the system property indicated by the specified key. * <p> * First, if a security manager exists, its * <code>SecurityManager.checkPermission</code> method * is called with a <code>PropertyPermission(key, "write")</code> * permission. This may result in a SecurityException being thrown. * If no exception is thrown, the specified property is removed from * the system properties. * <p> * * @param key the name of the system property. * @return the previous value of the system property, * or <code>null</code> if it did not have one. * @exception SecurityException if a security manager exists and its * <code>checkPermission</code> method doesn't allow * setting of the specified property. * @exception NullPointerException if <code>key</code> is * <code>null</code>. * @exception IllegalArgumentException if <code>key</code> is empty. * @see java.util.PropertyPermission * @see SecurityManager#checkPermission * @since 1.5 */ public static String clearProperty(String key) { if (key == null) { throw new NullPointerException("key can't be null"); } if (key.equals("")) { throw new IllegalArgumentException("key can't be empty"); } if (security != null) security.checkPermission(new PropertyPermission(key, "write")); return (String) props.remove(key); }
11-06-2004

WORK AROUND This code fragment will allow you to entirely remove a property. Properties p = System.getProperties(); p.remove(key); System.setProperties(p); Unfortunately, this still requires permission to read/write all of the system properties. -- iag@sfbay 2001-11-02
02-11-2001

EVALUATION The javadoc for setProperty() should be modified to indicate that a NullPointerException will be thrown if value is null. There is no API to handle removing an individual system property. To address this problem, a new method System.clearProperty(String key) should be added. -- iag@sfbay 2001-11-02
02-11-2001