Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top