How to get the current working directory in Java?

To get the current working directory in Java, you can use the System.getProperty("user.dir") method. This method returns the current working directory of the Java process as a string. Here's an example:

String currentWorkingDirectory = System.getProperty("user.dir");
System.out.println("Current working directory: " + currentWorkingDirectory);

Alternatively, you can use the Paths.get(".").toAbsolutePath().normalize() method to get the current working directory as a Path object, and then use the toString() method to convert it to a string:

Path currentWorkingDirectory = Paths.get(".").toAbsolutePath().normalize();
System.out.println("Current working directory: " + currentWorkingDirectory.toString());

Note that the user.dir property specifies the current working directory of the Java process, which may not be the same as the current directory of the shell that started the process.