Java – How to display all Environment variable

In Java, you can use System.getenv() to return a Map with all the environment variables.


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

1. Example

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

    }

}

Output


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. Sorting

Java 8 example to display all the environment variable in alphabetical order.

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

    }

}

Output


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

//...

References

  1. Java – How to display all System properties
  2. Java 8 – How to sort a Map
  3. Wikipedia – Environment variable

About the Author

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

Comments

avatar