How to Get Process Environment Variables at Runtime in Java


In this example, we will learn how to get process environment variables in Java at runtime.

Source Code

package com.beginner.examples;

import java.util.Map;
import java.util.Set;
 
public class JavaEnvExample {
 
    public static void main(String a[]){
        // new  ProcessBuilder
        ProcessBuilder pb = new ProcessBuilder();
        // get environment variables
        Map env = pb.environment();
        Set keys = env.keySet();
        // print environment variables
        for(String key:keys){
            System.out.println(key + " -> " + env.get(key));
        }
    }
}

Output:

configsetroot -> C:windowsConfigSetRoot
PROCESSOR_LEVEL -> 6
FP_NO_HOST_CHECK -> NO
SESSIONNAME -> Console
ALLUSERSPROFILE -> C:ProgramData
TVTCOMMON -> C:Program Files (x86)Common FilesLenovo
PROCESSOR_ARCHITECTURE -> AMD64
PSModulePath -> C:windowssystem32WindowsPowerShellv1.0Modules
SystemDrive -> C:
COMMPath -> C:Program FilesLenovoCommunications Utility
MAVEN_HOME -> D:JAVAENVapache-maven-3.5.2
TVT -> C:Program Files (x86)Lenovo
MOZ_PLUGIN_PATH -> D:Foxit Readerplugins
SWSHARE -> C:SWSHARE
AXIS2_HOME -> D:JAVAENVapache-axis2-1.7.7
USERNAME -> jiang
ProgramFiles(x86) -> C:Program Files (x86)
PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
ProgramData -> C:ProgramData
ProgramW6432 -> C:Program Files
HOMEPATH -> Usersjiang
PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
ProgramFiles -> C:Program Files
PUBLIC -> C:UsersPublic
windir -> C:windows
=:: -> ::
LOCALAPPDATA -> C:UsersjiangAppDataLocal
DisplayLinkConfigRoot -> C:Program FilesDisplayLink Core Software8.0.923.0
USERDOMAIN -> jiang-PC
LOGONSERVER -> JIANG-PC
JAVA_HOME -> D:JAVAENVjdk180
ANT_HOME -> D:JAVAENVapache-ant-1.10.1
APPDATA -> C:UsersjiangAppDataRoaming
READYAPPS -> C:ProgramDataLenovoReadyApps
windows_tracing_logfile -> C:BVTBinTestsinstallpackagecsilogfile.log
CommonProgramFiles -> C:Program FilesCommon Files
Path -> D:JAVAENVjdk180bin;D:JAVAENVapache-ant-1.10.1bin;D:JAVAENVapache-maven-3.5.2bin;C:WindowsSystem32;D:Gitcmd;C:TPSSENVmysql5623bin;D:Microsoft VS Codebin;C:Program Files (x86)QuickTimeQTSystem;;D:Microsoft VS Codebin;C:WindowsSystem32wbem
OS -> Windows_NT
windows_tracing_flags -> 3
COMPUTERNAME -> JIANG-PC
PROCESSOR_REVISION -> 4e03
CommonProgramW6432 -> C:Program FilesCommon Files
ComSpec -> C:windowssystem32cmd.exe
SystemRoot -> C:windows
TEMP -> C:UsersjiangAppDataLocalTemp
HOMEDRIVE -> C:
USERPROFILE -> C:Usersjiang
TMP -> C:UsersjiangAppDataLocalTemp
CommonProgramFiles(x86) -> C:Program Files (x86)Common Files
NUMBER_OF_PROCESSORS -> 4

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments