Java - すべてのシステムプロパティを表示する方法

Javaでは、 `System.getProperties()`を使ってすべてのシステムプロパティを取得できます。

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

1.例

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

    }

}

出力

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.並べ替え

すべてのシステムプロパティをアルファベット順に表示する例

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

    }

}

出力

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:

参考文献

doc - システムのプロパティ]。 link://java/java-properties-file-examples/[Javaプロパティファイル

例]。リンク://java8/java-8-how-to-sort-a-map/[Java 8 - マップのソート方法]

  1. link://java/java-how-to-display-all-environment-variable/[Java - どのようにして

すべての環境変数を表示する]