How to get the current working directory in Java?

I want to access my current working directory using Java.

My code:

 String currentPath = new java.io.File(".").getCanonicalPath();
 System.out.println("Current dir:" + currentPath);

 String currentDir = System.getProperty("user.dir");
 System.out.println("Current dir using System:" + currentDir);

Output:

Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32

My output is not correct because the C drive is not my current directory.

How to get the current directory?

2
25

Leave a Comment