In Java, System.getProperty(key) returns the value of the system property named key, or null if there is no such property. System.getProperty(key, def) returns def instead of null.
System properties are text settings inside the JVM. Java fills in standard ones at startup, such as java.version, os.name, user.home, and file.separator. You can add your own on the command line with java -Dapp.mode=test MyApp, which makes getProperty("app.mode") return "test".
A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.getProperty(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the key, and optionally a default value:
Java
String home = System.getProperty("user.home"); // the user's home folder
String mode = System.getProperty("app.mode"); // null if not set
String level = System.getProperty("app.level", "INFO"); // "INFO" if not set
Call
What it does
System.getProperty("java.version")
Version of the running Java, for example "21.0.2".
System.getProperty("user.dir")
The working directory the program was started in.
System.getProperty("app.mode", "prod")
Your own setting, with a fallback.
Boolean.getBoolean("app.debug")
true only if the property exists and equals "true" (ignoring case).
Keys are case-sensitive. Values are always Strings, so convert numbers yourself, or use Integer.getInteger(key, def) and Boolean.getBoolean(key), which read system properties for you.
Library declarations
Java
// java.lang.System
public static String getProperty(String key)
public static String getProperty(String key, String def)
Parameters
Parameter
Description
key
Name of the property, for example "user.home". Must not be null or empty.
def
(Second overload) Value to return when the property does not exist.
Return Value
Return Value
Description
String
The property value; null (first overload) or def (second overload) if the property is not set.
Shortcut for the line.separator value read at startup.
Integer.getInteger(key, def)
Reads a system property and parses it as an Integer.
Java Examples
Example 1 — Read Standard Properties Without Printing Machine Details
Standard properties always exist, but their values differ per computer, so the program only prints facts that are the same everywhere.
Java
public class SystemGetPropertyStandard {
public static void main(String[] args) {
String version = System.getProperty("java.version");
String home = System.getProperty("user.home");
String sep = System.getProperty("file.separator");
System.out.println("java.version present: " + (version != null));
System.out.println("user.home is not empty: " + !home.isEmpty());
System.out.println("file.separator length: " + sep.length());
System.out.println("Separator is / or \\: " + (sep.equals("/") || sep.equals("\\")));
}
}
Output
java.version present: true
user.home is not empty: true
file.separator length: 1
Separator is / or \: true
How It Works
Guaranteed keys.java.version, user.home, and file.separator are among the properties every JVM must define, so none of them is null.
Values stay private. Version and home folder differ per machine, so only != null and isEmpty() are printed.
One character either way. The separator is "/" on Linux and macOS and "\" on Windows, so its length is 1 and the either-or check is true. In Java source a single backslash is written "\\".
Example 2 — Configuration with Defaults and -D Options
Your own keys usually do not exist, so give a default. Running with -D options overrides them without changing code.
Java
public class SystemGetPropertyDefaults {
public static void main(String[] args) {
String mode = System.getProperty("app.mode");
String threads = System.getProperty("app.threads", "4");
int count = Integer.parseInt(threads);
System.out.println("app.mode: " + mode);
System.out.println("app.threads: " + threads);
System.out.println("Workers to start: " + count * 2);
System.out.println("Debug enabled: " + Boolean.getBoolean("app.debug"));
}
}
Terminal output
Run with plain java SystemGetPropertyDefaults, without any -D options.
No default, no value. Nothing set app.mode, so the one-argument call returns null, and string concatenation prints the word null.
The default fills the gap.getProperty("app.threads", "4") returns "4"; after Integer.parseInt the program doubles it to 8. Started as java -Dapp.threads=6 SystemGetPropertyDefaults, it would print 6 and 12.
A boolean shortcut.Boolean.getBoolean("app.debug") reads the system property and returns true only for the text "true", so a missing key gives false.
Example 3 — What Happens with an Empty or null Key
A missing key returns null, but an empty or null key is a programming error and throws.
A valid key. "java.version" always exists, so the lookup succeeds and only the presence check is printed.
Empty is not allowed. An empty key can never name a property, so getProperty("") throws IllegalArgumentException; the brackets show the empty string as [].
null is rejected first. A null key throws NullPointerException. One multi-catch block handles both exception types, and concatenating null prints [null].
Frequently Asked Questions
null with the one-argument version, or the default value you pass as the second argument.
Use the -D option before the class name: java -Dapp.mode=test MyApp. Then System.getProperty("app.mode") returns "test".
Among others java.version, java.vendor, java.home, os.name, os.arch, os.version, file.separator, path.separator, line.separator, user.name, user.home, and user.dir.
getProperty reads Java system properties that live inside the JVM and can be set with -D. getenv reads environment variables of the operating system, which are set before Java starts.
Because the key is an empty string. A null key throws NullPointerException instead.