Java – How to display all System properties

In Java, you can use System.getProperties() to get all the system properties.


	Properties properties = System.getProperties();
	properties.forEach((k, v) -> System.out.println(k + ":" + v)); // Java 8

1. Example

DisplayApp.java

package com.mkyong.display;

import java.util.Properties;

public class DisplayApp {

    public static void main(String[] args) {

        Properties properties = System.getProperties();
        // Java 8
        properties.forEach((k, v) -> System.out.println(k + ":" + v));

        // Classic way to loop a map
        //for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        //    System.out.println(entry.getKey() + " : " + entry.getValue());
        //}

        // No good, output is truncated, long lines end with ...
        //properties.list(System.out);
        
    }

}

Output


sun.desktop:windows
awt.toolkit:sun.awt.windows.WToolkit
java.specification.version:10
file.encoding.pkg:sun.io
sun.cpu.isalist:amd64
sun.jnu.encoding:Cp1252
java.class.path:D:\maven-examples\maven-profiles\target\classes;
java.vm.vendor:"Oracle Corporation"
sun.arch.data.model:64
user.variant:
java.vendor.url:http://java.oracle.com/
user.timezone:
os.name:Windows 10
java.vm.specification.version:10
sun.java.launcher:SUN_STANDARD
user.country:MY
sun.boot.library.path:C:\opt\Java\jdk-10\bin
sun.java.command:com.mkyong.password.DisplayApp
jdk.debug:release
sun.cpu.endian:little
user.home:C:\Users\mkyong
user.language:en
java.specification.vendor:Oracle Corporation
java.version.date:2018-04-17
java.home:C:\opt\Java\jdk-10
file.separator:\
java.vm.compressedOopsMode:Zero based
line.separator:

java.specification.name:Java Platform API Specification
java.vm.specification.vendor:Oracle Corporation
java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
user.script:
sun.management.compiler:HotSpot 64-Bit Tiered Compilers
java.runtime.version:10.0.1+10
user.name:mkyong
path.separator:;
os.version:10.0
java.runtime.name:Java(TM) SE Runtime Environment
file.encoding:UTF-8
java.vm.name:Java HotSpot(TM) 64-Bit Server VM
java.vendor.version:18.3
java.vendor.url.bug:http://bugreport.java.com/bugreport/
java.io.tmpdir:C:\Users\mkyong\AppData\Local\Temp\
java.version:10.0.1
user.dir:D:\maven-examples\maven-profiles
os.arch:amd64
java.vm.specification.name:Java Virtual Machine Specification
java.awt.printerjob:sun.awt.windows.WPrinterJob
sun.os.patch.level:
java.library.path:C:\opt\Java\jdk-10\bin;C:\WINDOWS\Sun\Java\bin;
java.vendor:Oracle Corporation
java.vm.info:mixed mode
java.vm.version:10.0.1+10
sun.io.unicode.encoding:UnicodeLittle
java.class.version:54.0

Process finished with exit code 0

2. Sorting

Example to display all the system properties in Alphabetical order.

DisplayApp.java

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));

    }

}

Output


awt.toolkit:sun.awt.windows.WToolkit
file.encoding:UTF-8
file.encoding.pkg:sun.io
file.separator:\
java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
java.awt.printerjob:sun.awt.windows.WPrinterJob

//...

sun.cpu.endian:little
sun.cpu.isalist:amd64
sun.desktop:windows
sun.io.unicode.encoding:UnicodeLittle
sun.java.command:com.mkyong.password.DisplayApp
sun.java.launcher:SUN_STANDARD
sun.jnu.encoding:Cp1252
sun.management.compiler:HotSpot 64-Bit Tiered Compilers
sun.os.patch.level:
user.country:MY
user.dir:D:\maven-examples\maven-profiles
user.home:C:\Users\mkyong
user.language:en
user.name:mkyong
user.script:
user.timezone:
user.variant:

References

  1. Oracle doc – System Properties
  2. Java Properties file examples
  3. Java 8 – How to sort a Map
  4. Java – How to display all Environment variable

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
mkyongKisnaNishant SonarEric WadsworthBob Recent comment authors
newest oldest most voted
Eric Wadsworth
Guest
Eric Wadsworth

This output is truncated, though. Some lines end in “…” instead of the rest of that property. Not so useful.

Kisna
Guest
Kisna

Prefer the -XX:+PrintFlagsFinal way!

Bob
Guest
Bob

Hi,

just wanted to say that I like your blog.

I’m not a coder, I’m an infrastructure guy and I found your posts very useful for Java tips which I need for a JMX monitoring tool that I’m writing.

Keep up the good work.

Regards,
Bob

Nishant Sonar
Guest
Nishant Sonar

Hi Bob

Did you write JMX bean for the system propertieS?

Regards
Nishant