In Java, System.setProperties(props) makes props the new set of system properties. From then on, getProperty and getProperties use that object. The old set is not merged in; it is replaced.
Passing null tells the JVM to forget the current set. In OpenJDK-based JVMs this creates a fresh set with the standard properties from startup, so keys added by your code disappear. Because replacing everything is risky, a common pattern is to build the new set on top of the old one with new Properties(defaults).
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.setProperties(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass a Properties object, or null to reset:
Java
Properties original = System.getProperties();
Properties layered = new Properties(original); // falls back to the originals
layered.setProperty("app.mode", "test");
System.setProperties(layered); // use the layered set
System.setProperties(original); // put the old set back
Call
What it does
System.setProperties(myProps)
Replaces all system properties with myProps.
System.setProperties(null)
Discards the current set; standard startup properties come back.
new Properties(System.getProperties())
An empty set whose lookups fall back to the current properties.
System.setProperties(new Properties())
Leaves no properties at all; even java.version reads null.
Keep a reference to the original object (System.getProperties()) before replacing it, so you can restore it, ideally in a finally block.
Library declarations
Java
// java.lang.System
public static void setProperties(Properties props)
Parameters
Parameter
Description
props
The new system properties, or null to reset to a new standard set.
Return Value
Return Value
Description
void
Nothing. Later getProperty calls read from the new set.
Before reset: x
After reset: null
java.version back: true
How It Works
A change to undo.setProperty("app.temp", "x") adds a key that did not exist at startup.
null means start over.setProperties(null) drops the whole current set, including "app.temp", so it reads null afterwards.
Standard keys are rebuilt. OpenJDK creates a fresh set with the startup properties, so java.version exists again; only that presence is printed.
Example 2 — Load Settings on Top of the System Properties
A Properties object with defaults answers from its own keys first and falls back to the original system properties. The original set is restored in finally.
Java
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
public class SystemSetPropertiesLayer {
public static void main(String[] args) throws IOException {
Properties original = System.getProperties();
Properties config = new Properties(original);
config.load(new StringReader("db.host=localhost\ndb.port=5432\n"));
System.setProperties(config);
try {
System.out.println("db.host: " + System.getProperty("db.host"));
System.out.println("db.port + 1: " + (Integer.getInteger("db.port") + 1));
System.out.println("java.version still visible: " + (System.getProperty("java.version") != null));
} finally {
System.setProperties(original);
}
System.out.println("db.host after restore: " + System.getProperty("db.host"));
}
}
Output
db.host: localhost
db.port + 1: 5433
java.version still visible: true
db.host after restore: null
How It Works
Config text to properties.load(new StringReader(...)) parses the same key=value lines a .properties file would contain, giving two keys.
Own keys first, then defaults. With config installed, "db.host" and "db.port" come from the loaded text, while java.version is found through the defaults, the original set.
Put the original back.setProperties(original) in finally restores the old object, which never contained "db.host", so it reads null again.
Example 3 — Why an Empty Properties Object Is Dangerous
With new Properties() there are no keys at all. The program looks at one value while the set is empty and restores the backup immediately.
Java
import java.util.Properties;
public class SystemSetPropertiesEmpty {
public static void main(String[] args) {
Properties backup = System.getProperties();
String versionWhileEmpty;
System.setProperties(new Properties());
try {
versionWhileEmpty = System.getProperty("java.version");
} finally {
System.setProperties(backup);
}
System.out.println("java.version while empty: " + versionWhileEmpty);
System.out.println("java.version after restore: " + (System.getProperty("java.version") != null));
System.out.println("Backup is active again: " + (System.getProperties() == backup));
}
}
Output
java.version while empty: null
java.version after restore: true
Backup is active again: true
How It Works
Nothing to fall back on. An empty Properties has no keys and no defaults, so even the always-present java.version reads null.
Keep the empty phase tiny. Only one lookup runs between replacing and restoring, so no other code can stumble over the missing standard keys.
The very same object returns. After setProperties(backup), getProperties() returns backup itself, so == is true and every original key is back.
Frequently Asked Questions
It replaces the whole set of system properties with the Properties object you pass. Later getProperty calls read from that object.
The current set is forgotten. OpenJDK-based JVMs then create a fresh set with the standard startup properties, so keys your program added are gone.
Call setProperty for each key, or create new Properties(System.getProperties()), add your keys to it, and pass it to setProperties.
Save System.getProperties() in a variable before the call and pass that variable to setProperties again, preferably in a finally block.
No. The JDK reads the line separator once at startup, so println keeps using the original line break.