Java properties: How to get the user’s home directory

Java properties FAQ: In Java, how do I determine a user’s home directory?

Answer: I often need to do this when creating Java/Swing GUI applications. Fortunately it's simple (once you know how to do it), just use a line of code like this, using the Java System class getProperty method:

String homeDir = System.getProperty("user.home");

On my Mac OS X system, the String homeDir now contains the following content:

/Users/alvin

Other Java system property examples

Of course the trick to this is knowing that there is a property named user.home. To see all the other Java system properties that are available to you, please refer to my "How to list all the Java system properties" tutorial.