Languages
[Edit]
EN

Java - how to print all system properties

5 points
Created by:
JustMike
26150

We can print Java system properties with System.getProperties() method.

The simples way to do it is with:

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

1. Print Java system properties with java 8 - forEach.

Code example:

import java.util.Properties;

public class JavaPrintAllPropertiesWithJava8 {

    public static void main(String[] args) {

        Properties properties = System.getProperties();

        properties.forEach((k, v) -> System.out.println(k + " : " + v));
    }
}

Output:

sun.cpu.isalist : amd64
sun.desktop : windows
sun.io.unicode.encoding : UnicodeLittle
sun.cpu.endian : little
java.vendor.url.bug : http://bugreport.sun.com/bugreport/
file.separator : \
java.vendor : Oracle Corporation
sun.boot.class.path : C:\Program Files\Java\jdk1.8.0_221\jre\lib\resourc...
java.ext.dirs : C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext;C:\Window....
java.version : 1.8.0_221
java.vm.info : mixed mode
awt.toolkit : sun.awt.windows.WToolkit
user.language : en
java.specification.vendor : Oracle Corporation
sun.java.command : java_.java_core.java_print_all_props._1__.JavaPrintAllPr...
java.home : C:\Program Files\Java\jdk1.8.0_221\jre
sun.arch.data.model : 64
java.vm.specification.version : 1.8
java.class.path : C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.j...
user.name : seth
file.encoding : UTF-8
java.specification.version : 1.8
java.awt.printerjob : sun.awt.windows.WPrinterJob
user.timezone : 
user.home : C:\Users\seth
os.version : 10.0
sun.management.compiler : HotSpot 64-Bit Tiered Compilers
java.specification.name : Java Platform API Specification
java.class.version : 52.0
java.library.path : C:\Program Files\Java\jdk1.8.0_221\bin;C:\Windows\Sun...
sun.jnu.encoding : Cp1252
os.name : Windows 10
user.variant : 
java.vm.specification.vendor : Oracle Corporation
java.io.tmpdir : C:\Users\seth\AppData\Local\Temp\
line.separator : 

java.endorsed.dirs : C:\Program Files\Java\jdk1.8.0_221\jre\lib\endorsed
os.arch : amd64
java.awt.graphicsenv : sun.awt.Win32GraphicsEnvironment
java.runtime.version : 1.8.0_221-b11
java.vm.specification.name : Java Virtual Machine Specification
user.dir : C:\idea_tmp\common_v5
user.country : US
user.script : 
sun.java.launcher : SUN_STANDARD
sun.os.patch.level : 
java.vm.name : Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg : sun.io
path.separator : ;
java.vm.vendor : Oracle Corporation
java.vendor.url : http://java.oracle.com/
sun.boot.library.path : C:\Program Files\Java\jdk1.8.0_221\jre\bin
java.vm.version : 25.221-b11
java.runtime.name : Java(TM) SE Runtime Environment

2. Print Java system properties with Enumeration

Code example:

import java.util.Enumeration;
import java.util.Properties;

public class JavaPrintAllProperties {

    public static void main(String[] args) {

        Properties properties = System.getProperties();
        Enumeration keys = properties.keys();

        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            String value = (String) properties.get(key);
            System.out.println(key + " : " + value);
        }
    }
}

Output:

The same as Example 1.

3. Print Java system properties in sorted order

Code example:

import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class JavaPrintAllPropertiesSortedOrder {

    public static void main(String[] args) {

        Properties properties = System.getProperties();
        Set<String> keys = properties.stringPropertyNames();
        SortedSet<String> keysSorted = new TreeSet<>(keys);

        for (String key : keysSorted) {
            String value = (String) properties.get(key);
            System.out.println(key + " : " + value);
        }
    }
}

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

...

os.arch : amd64
os.name : Windows 10
os.version : 10.0
path.separator : ;
sun.arch.data.model : 64

...

user.language : en
user.name : seth
user.script : 
user.timezone : 
user.variant : 

For the readability purpose I printed part of the output.

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join