Example to get user home directory in Java

Best way to get user’s home directory and current program execution directory in Java.

In java getting user’s profile home directory is quite simple using System.getProperty("user.home") and it works in most of the cases. There is another property user.dir which returns current directory of execution. So don’t get confuse as both property holds different paths.

Code example to get user home directory and current directory of executing program

public class UserHomeDirExample {
    public static void main(String[]args){
    public static String getUserHomeDir() {
        return Paths.get(System.getProperty("user.home")).toAbsolutePath()
    public static String getCurrentDir() {
        return Paths.get(System.getProperty("user.dir")).toAbsolutePath()