Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to remove a system property in java?

2,182 views
Skip to first unread message

pengwg

unread,
Oct 18, 2001, 5:35:50 AM10/18/01
to
Hi!

Can some tell me the easiest way to remove a system property. I tried with
System.setProperty("myProperty", null);
It didn't work.

Regards,
Wei

Jon Skeet

unread,
Oct 18, 2001, 5:43:33 AM10/18/01
to

What do you mean by "remove" in this case? What is end effect you want
to have? Just that System.getProperty will return null?

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Gordon Beaton

unread,
Oct 18, 2001, 7:58:14 AM10/18/01
to

Properties extends Hashtable, so
System.getProperties().remove("myProperty") might be what you are
looking for.

Keep in mind though that many of the properties are only informative,
so changing or removing them doesn't affect anything but the set of
properties itself.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

pengwg

unread,
Oct 18, 2001, 9:34:58 AM10/18/01
to
Yes, exactly. The reason is that after I modify a system property I
have to set it back to its original state. If this original state is
null (not defined), I get trouble. The setProperty() throws always an
exception. E.g:

String oldPropery = System.getProperty("somGlobalPropery");
System.setProeprty("somGlobalPropery", "myValue");
// do somthing
......
// set it back
System.setProeprty("somGlobalPropery", oldProperty); // fails if
oldProperty is null


Wei


Jon Skeet <sk...@pobox.com> wrote in message news:<MPG.1638b4a36...@mrmog.peramon.com>...

Jon Skeet

unread,
Oct 18, 2001, 9:49:52 AM10/18/01
to
pengwg <wei-gu...@sap.com> wrote:
> Yes, exactly. The reason is that after I modify a system property I
> have to set it back to its original state. If this original state is
> null (not defined), I get trouble. The setProperty() throws always an
> exception. E.g:
>
> String oldPropery = System.getProperty("somGlobalPropery");
> System.setProeprty("somGlobalPropery", "myValue");
> // do somthing
> ......
> // set it back
> System.setProeprty("somGlobalPropery", oldProperty); // fails if
> oldProperty is null

Right. What I suggest you do is use System.setProperties() instead -
when you want to make a change, do something like:

Properties previousProps = System.getProperties();
Properties newProps = new Properties (previousProps);

newProps.setProperty ("foo", "bar");
System.setProperties (newProps);

then later:

System.setProperties (previousProps);

Note that you'll get in trouble with this if you do this in different
places in an overlapping way.

yasmi...@gmail.com

unread,
Jan 15, 2019, 4:28:29 AM1/15/19
to
You can use:

System.clearProperty("myProperty");

"myProperty" being the key.
0 new messages