How to Get Current Working Directory in Java?

The following will print out the current directory from where the command was invoked regardless where the .class or .jar file the .class file is in.

package com.javaguides.collections.queueexamples;

public class CurrentWorkingDirExample {
 public static void main(final String[] args)
    {
        final String dir = System.getProperty("user.dir");
        System.out.println("current dir = " + dir);
    }
}

Output

current dir = C:\Java_Examples\sourcecodeexamples

Reference

Comments