How to get current working directory in java

This example shows you how to get the current working directory in java.

You can get the current directory from the key ‘user.dir’ using the System class.

Below is the example of the getting current directory in java.


//How to get current working directory in java
public class CurrentDirectoryExample {
	public static void main(String[] args) {
		// Use the key user.key to get the current directory
		String userDirKey = "user.dir";
		// The the property value from System
		String currentDirectory = System.getProperty(userDirKey);
		// Print the result
		System.out.println("The current directory is : " + currentDirectory);

The above example will print the current directory full path.