How can I get current working directory?

A system properties named user.dir can be used if you want to find the current working directory of your Java program.


public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // System property key to get current working directory.
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);

Result example:

Working Directory: /Users/wsaryada/Projects/kodejava