In this post, I will be sharing how to get environment variable in Java. There are two ways to get environment variables:
1. System.getenv(String variable) that returns specific environment variable value
2. System.getenv() that returns all environment variables values

Read Also: How to Print Screen in Java

What are environment variables?

Environment variables are key/value pairs just like properties in the Java platform where both the key and the value are strings.
A few Operating Systems use environment variables to transfer configuration information to applications.

Java Program to get the specific environment variable

System.getenv(String name) - We will use System.getenv(String name) to get the specified environment variable. In the below java program, we are getting the value of the "JAVA_HOME" environment variable.

 public class GetEnvironmentVariable {
public static void main(String args[]) {
System.out.println("Get Specific Environment Variable");
System.out.println("JAVA_HOME value is: " + System.getenv("JAVA_HOME"));
}
}

Output:
Get Specific Environment Variable
JAVA_HOME value is: C:\Program Files\Java\jdk1.8.0_291

Java Program to get all environment variables

System.getenv() - This method returns all environment variables. Since environment variables are string key/value pairs. This method will return all the environment variables as a map of variable names to values(Map<String,String>).

 import java.util.Map;
public class GetEnvironmentVariable2 {
public static void main(String args[]) {
System.out.println("Get All Environment Variables");
Map<String,String> mapObj = System.getenv();
for (Map.Entry<String,String> entry : mapObj.entrySet()) {
System.out.println("Key: " + entry.getKey()+ " Value: "+ entry.getValue());
}
}
}

Output:
 Get All Environment Variables
Key: MyOffice Value: BLR
Key: PROCESSOR_LEVEL Value: 6
Key: SESSIONNAME Value: Console
Key: ALLUSERSPROFILE Value: C:\ProgramData
Key: PROCESSOR_ARCHITECTURE Value: AMD64
Key: PSModulePath Value: C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft Azure Information Protection\Powershell
Key: SystemDrive Value: C:
Key: USERNAME Value: submittal
Key: ProgramFiles(x86) Value: C:\Program Files (x86)
Key: FPS_BROWSER_USER_PROFILE_STRING Value: Default
Key: PATHEXT Value: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
Key: DriverData Value: C:\Windows\System32\Drivers\DriverData
Key: ProgramData Value: C:\ProgramData
Key: ProgramW6432 Value: C:\Program Files
Key: HOMEPATH Value: \Users\SUBMITTAL
Key: MyScriptVer Value: DomainLogonVista.vbs-2a
Key: PROCESSOR_IDENTIFIER Value: Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
Key: ProgramFiles Value: C:\Program Files
Key: PUBLIC Value: C:\Users\Public
Key: windir Value: C:\WINDOWS
Key: =:: Value: ::\
Key: LOCALAPPDATA Value: C:\Users\SUBMITTAL\AppData\Local
Key: ChocolateyLastPathUpdate Value: 132690986583143576
Key: USERDOMAIN Value: US
Key: FPS_BROWSER_APP_PROFILE_STRING Value: Internet Explorer
Key: JAVA_HOME Value: C:\Program Files\Java\jdk1.8.0_291
Key: OneDrive Value: C:\Users\Administrator\OneDrive
Key: APPDATA Value: C:\Users\SUBMITTAL\AppData\Roaming
Key: ChocolateyInstall Value: C:\ProgramData\chocolatey
Key: MyFunction Value: CG
Key: CommonProgramFiles Value: C:\Program Files\Common Files
Key: Path Value: C:/Users/SUBMITTAL/Downloads/eclipse-jee-2021-06/eclipse//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_16.0.1.v20210528-1205/jre/bin;C:\Python39\Scripts\;C:\Python39\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\SUBMITTAL\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Users\SUBMITTAL\AppData\Local\Microsoft\WindowsApps;C:\Users\SUBMITTAL\AppData\Roaming\npm;C:\Users\SUBMITTAL\Downloads\eclipse-jee-2021-06\eclipse;
Key: NEXTHINK Value: C:\Program Files\Nexthink\Collector
Key: OS Value: Windows_NT
Key: CommonProgramW6432 Value: C:\Program Files\Common Files
Key: ComSpec Value: C:\WINDOWS\system32\cmd.exe
Key: SystemRoot Value: C:\WINDOWS
Key: TEMP Value: C:\Users\SUBMIT~1\AppData\Local\Temp
Key: HOMEDRIVE Value: C:
Key: USERPROFILE Value: C:\Users\SUBMITTAL
Key: TMP Value: C:\Users\SUBMIT~1\AppData\Local\Temp
Key: CommonProgramFiles(x86) Value: C:\Program Files (x86)\Common Files
Key: NUMBER_OF_PROCESSORS Value: 8


That's all for today. Please mention in the comments in case you have any other questions related to getting the environment variables in Java.