Java Program to Find Current Working Directory

Last Updated : 14 Sep, 2026

The System.getProperty() method in Java is used to retrieve the value of a system property using its property key. If the specified property does not exist, the method returns null.

  • The key parameter identifies the system property whose value needs to be retrieved.
  • System.getProperty() returns the property value as a String.
  • The user.dir property returns the current working directory of the Java application.

Syntax

public static String getProperty(String key)

Parameter: key – the name of the system property to retrieve.

Returns:

  • The value of the specified system property as a String.
  • null if no property exists with the specified key.

Working of System.getProperty()

System.getProperty() uses a property key to look up the corresponding system property and returns its value as a String.

  • Provide the Key: Pass the name of the required system property to getProperty().
  • Search the Property: Java looks for a system property associated with the specified key.
  • Retrieve the Value: If the property exists, its value is returned as a String.
  • Handle Missing Property: If no property exists with the specified key, the method returns null.

Example: Program uses System.getProperty() with the user.dir key to retrieve the current working directory.

Java
public class GFG {
    public static void main(String[] args) {

        String path = System.getProperty("user.dir");

        System.out.println("Working Directory = " + path);
    }
}

Output:

Screenshot

Explanation: The System.getProperty("user.dir") call retrieves the current working directory stored in the user.dir system property. The returned value is stored in the path variable and printed to the console.

Note: The output path depends on the directory from which the Java program is executed.

Common System Properties

Java provides several predefined system properties that provide information about the current environment.

Property Description
user.dirCurrent working directory
user.nameCurrent user's name
user.homeUser's home directory
java.versionJava runtime version
os.nameOperating system name
os.archOperating system architecture
file.separatorFile separator used by the operating system
path.separatorPath separator used by the operating system
Comment