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);


current dir = C:\Java_Examples\sourcecodeexamples


Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course