10

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?

xlecoustillier
  • 15,451
  • 14
  • 56
  • 79
miku
  • 161,705
  • 45
  • 286
  • 300
  • i think these are keys – anshulkatta Jun 10 '13 at 12:25
  • @anshulkatta; I guess the keys always exist - but the question is, do they always point to something (sane?). – miku Jun 10 '13 at 12:26
  • @miku keys only exist when there is a value, check my answer below – DevZer0 Jun 10 '13 at 12:28
  • `user.home` is os dependent but it rarely gets it wrong – fge Jun 10 '13 at 12:28
  • 1
    @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? – miku Jun 10 '13 at 12:32
  • 1
    Well, you don't have to, you can just `Files.createDirectories()`; if it fails you know the JVM lies to you ;) – fge Jun 10 '13 at 12:33

4 Answers4

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.

Harald K
  • 23,992
  • 6
  • 49
  • 97
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

DevZer0
  • 13,069
  • 5
  • 24
  • 48
  • 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? – Peter Jaloveczki Jun 10 '13 at 12:37
  • @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`. – miku Jun 10 '13 at 12:44
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.

Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
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.

Community
  • 1
  • 1
Santosh
  • 16,973
  • 4
  • 50
  • 75
  • The link to the question gave me an answer: [System.getProperties](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#getProperties%28%29): *This set of system properties* **always includes values** *for the following keys*: ... `user.home` ... – miku Jun 10 '13 at 21:07