In Java, System.getProperties() returns the entire set of system properties as a java.util.Properties object: the standard keys such as java.version and user.home, every -D option, and every key set by code.
It is the live object the JVM uses, not a copy. A later setProperty call shows up in it, and writing into it changes the system properties. Use it to list or search properties; for one key, getProperty is simpler.
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.getProperties(...). No import is needed, because classes in java.lang are always visible.
Syntax
No arguments; store the result in a Properties variable:
Java
Properties props = System.getProperties();
String home = props.getProperty("user.home");
Set<String> names = props.stringPropertyNames(); // all keys (a copy)
props.list(System.out); // dump everything (debugging)
Call
What it does
System.getProperties()
The live Properties object with all keys.
props.stringPropertyNames()
A snapshot Set of all keys whose values are strings.
props.containsKey("java.version")
Checks whether a key exists.
props.list(System.out)
Prints every property; values are cut off after 40 characters.
Properties extends Hashtable, so its keys have no fixed order. Sort the names yourself, for example with a TreeSet, when the order matters.
Library declarations
Java
// java.lang.System
public static Properties getProperties()
Parameters
Parameter
Description
None
getProperties() takes no parameters.
Return Value
Return Value
Description
Properties
The current system properties. The same object is returned each time until setProperties replaces it.
Has java.version: true
app.mode before: null
app.mode after: demo
Same object each call: true
How It Works
All keys in one object.containsKey("java.version") is true because every JVM defines that standard key at startup.
Not a snapshot.props was fetched before "app.mode" existed, yet after System.setProperty it returns "demo": both work on the same object.
One shared instance. A second getProperties() call returns the very same object, so == is true.
Example 2 — List Your Own Keys in Sorted Order
Collect the keys that start with "shop." into a TreeSet, which keeps them sorted, then print each key and value.
Java
import java.util.Properties;
import java.util.TreeSet;
public class SystemGetPropertiesPrefix {
public static void main(String[] args) {
System.setProperty("shop.name", "Corner Store");
System.setProperty("shop.currency", "EUR");
System.setProperty("shop.open", "09:00");
Properties props = System.getProperties();
TreeSet<String> names = new TreeSet<>();
for (String name : props.stringPropertyNames()) {
if (name.startsWith("shop.")) {
names.add(name);
}
}
for (String name : names) {
System.out.println(name + " = " + props.getProperty(name));
}
System.out.println("More than 3 properties in total? " + (props.size() > 3));
}
}
Output
shop.currency = EUR
shop.name = Corner Store
shop.open = 09:00
More than 3 properties in total? true
How It Works
A safe copy of the keys.stringPropertyNames() returns a new set of all keys, including the dozens of standard ones, that is not affected by later changes.
Filter and sort. Only names starting with "shop." go into the TreeSet, which orders them alphabetically: currency, name, open. A plain loop over props would have no fixed order.
Many more keys. Besides the three shop keys, the standard properties are in the same object, so props.size() > 3 is true.
Example 3 — Check the Keys Every JVM Must Provide
The documentation lists properties that are always present. The program counts how many of them it finds, without printing any values.
Java
import java.util.Properties;
public class SystemGetPropertiesKeys {
public static void main(String[] args) {
String[] keys = {"java.version", "java.vendor", "java.home", "os.name", "os.arch",
"file.separator", "path.separator", "line.separator", "user.name", "user.home", "user.dir"};
Properties props = System.getProperties();
int found = 0;
for (String key : keys) {
if (props.getProperty(key) != null) {
found++;
}
}
System.out.println("Standard keys checked: " + keys.length);
System.out.println("Standard keys found: " + found);
System.out.println("Made-up key found: " + props.containsKey("codetofun.not.a.key"));
}
}
Output
Standard keys checked: 11
Standard keys found: 11
Made-up key found: false
How It Works
A checklist. The array holds 11 of the keys that the getProperties documentation promises, covering Java, the operating system, separators, and the user.
All present. Every one of them has a value on every JVM, so all 11 are found; the values themselves stay unprinted because they differ per machine.
Unknown keys are simply absent.containsKey returns false for a name nobody defined, without throwing.
Frequently Asked Questions
A java.util.Properties object containing every system property: the standard ones, -D options, and keys set by your code.
No. It returns the live object the JVM uses, so later setProperty calls are visible in it and changes to it affect getProperty.
Call System.getProperties().list(System.out), or loop over stringPropertyNames(), sorted with a TreeSet, and print each key with getProperty.
Properties is a kind of Hashtable, which does not keep keys in order. Sort the key names first if you need a stable order.
java.version, java.vendor, java.vendor.url, java.home, java.class.version, java.class.path, os.name, os.arch, os.version, file.separator, path.separator, line.separator, user.name, user.home, and user.dir, among others.