Java - すべての環境変数を表示する方法

Javaでは、 `System.getenv()`を使用してすべての環境変数で `Map`を返すことができます。

    Map<String, String> env = System.getenv();
    env.forEach((k, v) -> System.out.println(k + ":" + v));

1.例

DisplayApp.java

package com.mkyong.display;

import java.util.Map;

public class DisplayApp {

    public static void main(String[]args) {

        Map<String, String> env = System.getenv();
       //Java 8
       //env.forEach((k, v) -> System.out.println(k + ":" + v));

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

    }

}

出力

USERDOMAIN__ROAMINGPROFILE : DESKTOP-20APO9O
PROCESSOR__LEVEL : 6
SESSIONNAME : Console
ALLUSERSPROFILE : C:\ProgramData
PROCESSOR__ARCHITECTURE : AMD64
SystemDrive : C:
MAVEN__HOME : C:\opt\apache-maven-3.5.3
NGINX : C:\opt\nginx-1.12.2
USERNAME : mkyong
GPU__USE__SYNC__OBJECTS : 1
ProgramFiles(x86) : C:\Program Files (x86)
GPU__MAX__ALLOC__PERCENT : 100
PATHEXT : .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
DriverData : C:\Windows\System32\Drivers\DriverData
ProgramData : C:\ProgramData
ProgramW6432 : C:\Program Files
HOMEPATH : \Users\mkyong
PROCESSOR__IDENTIFIER : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
ProgramFiles : C:\Program Files
PUBLIC : C:\Users\Public
windir : C:\WINDOWS
=:: : ::\
GPU__MAX__HEAP__SIZE : 100
LOCALAPPDATA : C:\Users\mkyong\AppData\Local
GPU__SINGLE__ALLOC__PERCENT : 100
GPU__FORCE__64BIT__PTR : 0
//...

2.並べ替え

すべての環境変数をアルファベット順に表示するJava 8の例

DisplayApp.java

package com.mkyong.display;

package com.mkyong.password;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class DisplayApp {

    public static void main(String[]args) {

        Map<String, String> env = System.getenv();

        LinkedHashMap<String, String> collect =
                env.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));

    }

}

出力

ALLUSERSPROFILE:C:\ProgramData
APPDATA:C:\Users\mkyong\AppData\Roaming
COMPUTERNAME:DESKTOP-20APO9O
ComSpec:C:\WINDOWS\system32\cmd.exe
CommonProgramFiles:C:\Program Files\Common Files
CommonProgramFiles(x86):C:\Program Files (x86)\Common Files
CommonProgramW6432:C:\Program Files\Common Files
DriverData:C:\Windows\System32\Drivers\DriverData
GPU__FORCE__64BIT__PTR:0
GPU__MAX__ALLOC__PERCENT:100
GPU__MAX__HEAP__SIZE:100
GPU__SINGLE__ALLOC__PERCENT:100
GPU__USE__SYNC__OBJECTS:1
GRADLE__HOME:C:\opt\gradle-4.7
HOMEDRIVE:C:
HOMEPATH:\Users\mkyong
JAVA__HOME:C:\opt\Java\jdk-10
LOCALAPPDATA:C:\Users\mkyong\AppData\Local
LOGONSERVER:\\DESKTOP-20APO9O
MAVEN__HOME:C:\opt\apache-maven-3.5.3
MONGODB__HOME:C:\Program Files\MongoDB\Server\3.6
NGINX:C:\opt\nginx-1.12.2
NUMBER__OF__PROCESSORS:8
OS:Windows__NT
OneDrive:C:\Users\mkyong\OneDrive
PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PHP:C:\opt\php-7.2.0
PROCESSOR__ARCHITECTURE:AMD64
PROCESSOR__IDENTIFIER:Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
PROCESSOR__LEVEL:6
PROCESSOR__REVISION:9e09
//...

参考文献

  1. link://java/how-to-list-all-system-properties-key-and-value-in-java/[Java

  2. https://en.wikipedia.org/wiki/Environment__variable [ウィキペディア -

環境変数]

環境変数 リンク://タグ/java/[java] map sorting

前の投稿:Spring起動時のSpringデータJPA Oracleの例
次の投稿:RESTEasyでのファイルアップロードの例