Are Java system properties always non-null?
Asked Answered
G

4

9

There are a couple of Java system properties, among them things like java.home and path.separator, user.home. The spec does not mention any formal promises on the existence of those values.

Especially I am interested in user.home. Does it always point to some existing path?

Gestalt answered 10/6, 2013 at 12:22 Comment(6)
i think these are keysConnoisseur
@anshulkatta; I guess the keys always exist - but the question is, do they always point to something (sane?).Gestalt
@Gestalt keys only exist when there is a value, check my answer belowAnchylose
user.home is os dependent but it rarely gets it wrongHerder
@fge, yes that is my impression, too. I'm just curious under which circumstances a default value might not be set. My use case: app creates a ~/.mycache file on startup; do I need to explicitly check for the existence of user.home beforehand?Gestalt
Well, you don't have to, you can just Files.createDirectories(); if it fails you know the JVM lies to you ;)Herder
T
4

I think you can safely assume that all the properties in that list is always available in any recent (Oracle-provided) JVM.

However, a null check is more defensive, and not expensive in this case.

I've never seen user.home to be null or be incorrectly specified by default. However, keep in mind that users can override with -Duser.home=..., so you can't rely on it to point to an existing path.

Tinnitus answered 10/6, 2013 at 12:31 Comment(1)
+1 for pointing out that user can set any property to something nonsensical.Ignaciaignacio
A
3

The documentation you pointed out states

The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

So if the property does not exist you get a null value

Anchylose answered 10/6, 2013 at 12:26 Comment(2)
I'm not sure that is what he meant. Surely the Java system property "mambo.jumbo" will be null no problem, but is it possible for a regular property to be uninitialized?Igneous
@PeterJaloveczki: Yes, I am more interested in the conditions when the "default" properties are not defined (not considering the case, when the user set them explicitly to garbage, like application.jar -Duser.home=good/luck.Gestalt
D
1

The spec says that user.home contains user home directory, it does not say it may contain null. I have no doubt that JVM guarantees that it is always set.

Deste answered 10/6, 2013 at 12:46 Comment(0)
P
1

The default properties will differ depending on OS. There will be some keys for which no values are defined. On my machine I found user.variant and user.timezone without any values! Following is the code which will list down all the key value pairs:

Properties prop = System.getProperties();     
      Set<Object> set = prop.keySet();    
      Iterator<Object> itr = set.iterator();

      while(itr.hasNext()){

          Object obj = itr.next();
          String propVal = System.getProperty(obj.toString());  
              System.out.println(obj.toString()+" = "+propVal);

      }
    }

Regarding your specific reference about user.home, it seems it defined most of the time. Check out this interesting post where people have posted the list of system properties on different machines.

Pia answered 10/6, 2013 at 13:25 Comment(1)
The link to the question gave me an answer: System.getProperties: This set of system properties always includes values for the following keys: ... user.home ...Gestalt

© 2022 - 2024 — McMap. All rights reserved.