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.
public class GFG {
public static void main(String[] args) {
String path = System.getProperty("user.dir");
System.out.println("Working Directory = " + path);
}
}
Output:

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.dir | Current working directory |
| user.name | Current user's name |
| user.home | User's home directory |
| java.version | Java runtime version |
| os.name | Operating system name |
| os.arch | Operating system architecture |
| file.separator | File separator used by the operating system |
| path.separator | Path separator used by the operating system |