To fix that, we can take a snapshot of system property keys:

final Properties systemProperties = System.getProperties()
final Set<String> keys = systemProperties.stringPropertyNames()

for (final String key : keys) {
    System.out.println("key: " + key)
    final String value = systemProperties.getProperty(key)
    System.out.println("value: " + value) // value can be null!
}

Notice the value comment - while stringPropertyNames states set of keys in this property list where the key and its corresponding value are strings, the system property could have been changed in the meantime.


Why so much legwork?

System properties are an instance of java.util.Properties, and its methods getProperty, setProperty are thread-safe.

Unfortunately, Properties' entry set's iterator (which I had used in question) is not thread-safe:

If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined

So actually when I was iterating over that map, some system property was modified (= that entry said was modified), which caused CME to be thrown.


This q&a pair also applies to any generic Properties usage - just system properties make it trickier, with ability to access them directly with statics such as java.lang.System.setProperty(String, String) - so controlling all accesses (especially in shared code) gets harder.

Answer from Adam Kotwasinski on Stack Overflow
Top answer
1 of 2
6

To fix that, we can take a snapshot of system property keys:

final Properties systemProperties = System.getProperties()
final Set<String> keys = systemProperties.stringPropertyNames()

for (final String key : keys) {
    System.out.println("key: " + key)
    final String value = systemProperties.getProperty(key)
    System.out.println("value: " + value) // value can be null!
}

Notice the value comment - while stringPropertyNames states set of keys in this property list where the key and its corresponding value are strings, the system property could have been changed in the meantime.


Why so much legwork?

System properties are an instance of java.util.Properties, and its methods getProperty, setProperty are thread-safe.

Unfortunately, Properties' entry set's iterator (which I had used in question) is not thread-safe:

If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined

So actually when I was iterating over that map, some system property was modified (= that entry said was modified), which caused CME to be thrown.


This q&a pair also applies to any generic Properties usage - just system properties make it trickier, with ability to access them directly with statics such as java.lang.System.setProperty(String, String) - so controlling all accesses (especially in shared code) gets harder.

2 of 2
0

You could wrap your properties in a ConcurrentHashMap so that any of your compound actions such as iteration, navigation, check-and-act, etc.. will be thread-safe. e.g.

ConcurrentHashMap<String, String> props = new ConcurrentHashMap<>(
    (Map<String, String>)(Object)System.getProperties());

for(Map.Entry<String, String> entry: props.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

Note that the iterators returned by the ConcurrentHashMap are weekly consistent, which means it may or may not reflect the changes to the collection after the construction of the iterator. If this is not what you wanted, you could use the Collections.synchronizedMap() instead, which pays some penalties in concurrency.

🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › sysprop.html
System Properties (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The setProperties method changes the set of system properties for the current running application. These changes are not persistent. That is, changing the system properties within an application will not affect future invocations of the Java interpreter for this or any other application.
🌐
Mkyong
mkyong.com › home › java › java – how to display all system properties
Java - How to display all System properties - Mkyong.com
October 26, 2018 - Example to display all the system properties in Alphabetical order. ... package com.mkyong.display; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; public class DisplayApp { public static void main(String[] args) { Properties properties = System.getProperties(); // Thanks Java 8 LinkedHashMap<String, String> collect = properties.entrySet().stream() .collect(Collectors.toMap(k -> (String) k.getKey(), e -> (String) e.getValue())) .entrySet().stream().sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); collect.forEach((k, v) -> System.out.println(k + ":" + v)); } }
🌐
Ispycode
ispycode.com › Java › System › Iterate-through-system-properties
Iterate through system properties | Java code
import java.util.Enumeration; import java.util.Properties; public class Example { public static void main(String[] args) { Properties props = System.getProperties(); Enumeration e = props.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); System.out.println(key + " -- " + props.getProperty(key)); } } }
🌐
Boraji
boraji.com › how-to-iterate-properites-in-java
https://boraji.com/how-to-iterate-properites-in-java
March 1, 2017 - In this post, we will show you ... method ( Introduced in Java 8) Iterating Properties using Enumeration The propertyNames() method of the Properties return an enumeration of all the keys....
🌐
Baeldung
baeldung.com › home › java › java system.getproperty vs system.getenv
Java System.getProperty vs System.getenv | Baeldung
July 25, 2025 - System properties are typically used to configure JVM-specific parameters, such as file encoding, user directories, JVM version, and other Java-specific configurations.
Find elsewhere
🌐
Oracle
docs.oracle.com › cd › E19717-01 › 819-7756 › gblxs › index.html
To Iterate Through a Properties Handle (Sun Java System Message Queue 4.1 Developer's Guide for C Clients)
Documentation Home > Sun Java System Message Queue 4.1 Developer's Guide for C Clients > Chapter 2 Using the C API > Working With Properties > Getting Message Properties > To Iterate Through a Properties Handle
🌐
How to do in Java
howtodoinjava.com › home › java basics › java system properties
Java System Properties
February 23, 2023 - For example, one such system property is “java.version”=”1.7.0_09“. We can retrieve all the system properties via System.getProperties() or we can also retrieve individual property via System.getProperty(key) method.
🌐
Coderanch
coderanch.com › t › 599586 › java › Properties-Class
for-each and Properties Class [Solved] (Beginning Java forum at Coderanch)
December 5, 2012 - how to get system information(like ram size,OS,OS version) using ip address in java · need help in properties file- key value pair · need help with Collections · Obtaining system properties · Using Properties instead of HashMap when dealing with Strings ·
🌐
Jenkov
jenkov.com › tutorials › java-collections › properties.html
Java Properties
Here is an example of removing a property from a Java Properties instance: ... You can iterate the keys of a Java Properties instance by obtaining the key set for the Properties instance, and iterating this key set.
🌐
Whitman College
whitman.edu › mathematics › java_tutorial › java › system › properties.html
System Properties
Your Java programs can read or write system properties through several methods in the System class. You can use a key to look up one property in the properties list, or you can get the whole set of properties all at once.
🌐
Blogger
sailendra-jena.blogspot.com › 2013 › 04 › how-to-iterate-properties-files-in-java.html
Java By Sailendra: How to iterate Properties Files in Java?
April 23, 2013 - 1. Properties props = System.getProperties(); for (String key : props .stringPropertyNames()) { System.out.println(key + " = " + props .getProperty(key)); } 2. Properties props = System.getProperties(); Enumeration e = props.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); System.out.println(key + " = " + props.getProperty(key)); } 3. Properties props = System.getProperties(); SortedMap sortedSystemProperties = new TreeMap(props); Set keySet = sortedSystemProperties.keySet(); Iterator iterator = keySet.iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); System.out.println(key + " = " + props .getProperty(key)); }
🌐
Tabnine
tabnine.com › home page › code › java › java.util.properties
Java Examples & Tutorials of Properties.forEach (java.util) | Tabnine
public Props(Properties props) { this.properties = new Properties(); props.forEach((k, v) -> this.properties.put(k.toString().trim(), v == null ? null : v.toString().trim())); this.encryption = new Encryption(props.getProperty(AesCipher.ENCRYPTION_SECRET_KEY_PATH)); } ... @Override public Map<String, String> getProperties() { Map<String, String> result = new HashMap<>(); loadAll(result); systemProps.forEach((key, value) -> result.put((String) key, (String) value)); return unmodifiableMap(result); }
🌐
Coderanch
coderanch.com › t › 397254 › java › Property-File-Iteration
Property File Iteration (Beginning Java forum at Coderanch)
September 29, 2004 - I'm trying to get a list of the key/value pairs in a property file without using the java.util.Properities.list() method. This is what I have thus far:.