掘图志

掘图志

 
 

Java How to display all Environment variable

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

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

上一篇: Java 8 Stream Read a file line by line
下一篇: Jackson 2 Convert Java Object to / from JSON
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号