Java System getProperty() Method
Example
Print the Java version, and a property that does not exist:
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("app.color"));
System.out.println(System.getProperty("app.color", "blue"));
}
}
Note: The output depends on the computer and its settings.
Definition and Usage
The getProperty() method returns the value of a system property. System properties are pairs of names (keys) and values that describe the system and the Java installation, for example "java.version", "os.name" and "user.home".
If the property does not exist, the method returns null, or the default value if you give one.
Tip: Use the setProperty() method to set the value of a system property.
Syntax
One of the following:
public static String getProperty(String key)
public static String getProperty(String key, String def)
Parameter Values
| Parameter | Description |
|---|---|
| key | Required. The name of the system property. |
| def | Optional. The default value, which is returned if the property does not exist. |
Technical Details
| Returns: | A String with the value of the property. If the property does not exist: null, or the default value. |
|---|
Related Pages
❮ System Methods