System.getProperty()
Returns a JVM system property: the Java version, the OS name, or your own property set with -D.
Available since: Java 1.0
Syntax
public static String getProperty(String key, String def)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | String | The property name, for example java.version or app.mode. |
| def | String | An optional default value used when the property is not set. |
Return value
StringThe property value, the default value, or null.
Examples
Standard JVM properties (the values depend on the machine):
void main() {
IO.println(System.getProperty("java.version"));
IO.println(System.getProperty("os.name"));
}Output:
25.0.4.1
Windows 10Pass your own property with -D before the file name: java -Dapp.mode=test Main.java. The second argument is the default value:
void main() {
IO.println(System.getProperty("app.mode", "dev"));
IO.println(System.getProperty("app.debug", "false"));
}Output:
test
falseNotes
Properties belong to the JVM, environment variables belong to the operating system; they are read by different methods. Useful keys: java.version, os.name, user.home, user.dir (the current folder), line.separator. System.setProperty() changes a property while the program runs. Ready-made helpers: Integer.getInteger("app.port", 8080) and Boolean.getBoolean("app.debug") read a property and convert it to a number or a boolean.
See also
- System.getenv() — Returns the value of an environment variable, or null if it is not set.
- System.exit() — Terminates the program immediately with the given exit code.
- Integer.parseInt() — Converts a string containing an integer to an int.
Related recipes
In the tutorial
- Arguments, environment and exit codes — How a Java program gets settings from outside — command-line arguments, environment variables with System.getenv, system properties with System.getProperty and -D, and exit codes with System.exit.