Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java System.getProperty() and getenv()

Reference for System.getProperty() and System.getenv(): system properties, -D flags, standard keys, environment variables, defaults and parsing helpers.

Java programs can read two kinds of external settings. System properties are string key/value pairs kept inside the JVM: some are set by Java itself (java.version, user.dir, line.separator), and you can add your own with -Dname=value on the command line or System.setProperty() at runtime. Environment variables belong to the operating system process and are read-only from Java.

Java
String version = System.getProperty("java.version");
String port = System.getProperty("server.port", "8080");
String home = System.getenv("HOME");

Try it

Java Reading and setting properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Environment variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static String getProperty(String key)
static String getProperty(String key, String def)
static String setProperty(String key, String value)
static String clearProperty(String key)
static Properties getProperties()
static String getenv(String name)
static Map<String, String> getenv()
Name Description
key / name The property or variable name. Must not be null; property keys must not be empty.
def Value returned when the property is missing.
Returns The value, or null (property or variable not defined). setProperty and clearProperty return the previous value or null.
Throws NullPointerException for a null key, IllegalArgumentException for an empty property key, SecurityException if a security manager forbids access (no longer relevant in current Java).

Frequently useful property keys:

Key Meaning
java.version, java.vendor, java.home The runtime.
os.name, os.arch, os.version The platform.
user.name, user.home, user.dir The user and the current working directory.
file.separator, path.separator, line.separator Platform separators.
java.io.tmpdir Temporary directory.
file.encoding, native.encoding, stdout.encoding Character sets (the default is UTF-8 since Java 18).

Usage notes

  • Strings only. Use Integer.getInteger("key", default), Long.getLong and Boolean.getBoolean("key") to read numeric and boolean properties. Note that these names are easy to confuse with Integer.parseInt: they look up a property, not parse the argument.
  • Setting from the command line: java -Dapp.mode=prod Main.java. Everything after the main class or JAR goes to args, not to properties.
  • Properties are global and mutable. Changing one affects the entire JVM, including libraries. Treat setProperty as a last resort and avoid it in multi-threaded code.
  • Environment variables are read-only. The map returned by getenv() is unmodifiable, and Java has no setenv. To pass variables to a child process, change ProcessBuilder.environment().
  • Names are case-sensitive on Linux and macOS but case-insensitive on Windows, where PATH and Path refer to the same variable.
  • Configuration advice. Properties suit JVM-level switches; environment variables suit deployment settings such as ports and URLs. Never print them wholesale: they often contain secrets.
  • Spelling counts. A misspelled key returns null quietly; supply a default whenever a missing value is acceptable.