Java System getProperty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.System

Definition

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
CallWhat 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

ParameterDescription
keyName 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 ValueDescription
StringThe property value; null (first overload) or def (second overload) if the property is not set.

Compare with Similar Methods

Other ways to read settings:

MethodHow it differs
getProperties()All properties at once as a Properties object.
getenv(name)Environment variables of the operating system, a separate set of names.
lineSeparator()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("\\")));
    }
}

How It Works

  1. Guaranteed keys. java.version, user.home, and file.separator are among the properties every JVM must define, so none of them is null.
  2. Values stay private. Version and home folder differ per machine, so only != null and isEmpty() are printed.
  3. 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"));
    }
}

How It Works

  1. No default, no value. Nothing set app.mode, so the one-argument call returns null, and string concatenation prints the word null.
  2. 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.
  3. 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.

Java
public class SystemGetPropertyErrors {
    public static void main(String[] args) {
        String[] keys = {"java.version", "", null};
        for (String key : keys) {
            try {
                String value = System.getProperty(key);
                System.out.println("[" + key + "] found: " + (value != null));
            } catch (NullPointerException | IllegalArgumentException e) {
                System.out.println("[" + key + "] " + e.getClass().getSimpleName());
            }
        }
    }
}

How It Works

  1. A valid key. "java.version" always exists, so the lookup succeeds and only the presence check is printed.
  2. Empty is not allowed. An empty key can never name a property, so getProperty("") throws IllegalArgumentException; the brackets show the empty string as [].
  3. 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.

Next: getSecurityManager()

Get the installed SecurityManager, which is null on modern Java (deprecated).

getSecurityManager() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful