How to Get an Environment Variable in Java

Each programming language for computers has variables and constants that allot particular memory addresses containing data that the programs can utilize. While the value of a variable can be altered, constant values cannot. The key/value combination known as an environment variable is one whose value is set externally to the program but whose reference is always available to the program at all times.

Value and key are both strings. Operating systems and command line interpreters always have different conventions for setting and using environment variables. Every application running on the system has constant access to them worldwide.

Usecase:

As far as we know, each time a program update is made, the server must be restarted or run, which could have unintended consequences when used in production. Therefore, the major goal of introducing environment variables is to repeatedly limit this execution and deployment.

Example:

Explanation:

In this case, Path is an environment variable that is set externally to the program but is accessible to all Windows-based programs.

Java provides 2 ways to get environment variables in the Java program.

  1. getenv()
  2. getProperty()

System.getenv():

When a specific key name is sent in as an argument, the System.getenv() method will retrieve the value of that key instead of just retrieving all the environment variables. java.lang. The string map of all the accessible environment variables is what System.getenv() always returns.

Syntax:

Name is the parameter used to define the key for which the value was required in this instance.

Returns:

If the supplied parameter is not linked to any keys, it will return null. Otherwise it will return the value of the key. All key/value pairs that are accessible will be returned if the parameter is not passed.

Implementation:

FileName: SystemGetEnv.java

Output:

System.getenv(PATH1) = null
System.getenv(TEMP1) = null
USERDOMAIN_ROAMINGPROFILE=DESKTOP-2CFH9HQ
LOCALAPPDATA=C:\Users\Nikhil\AppData\Local
PROCESSOR_LEVEL=6
USERDOMAIN=DESKTOP-2CFH9HQ
FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
LOGONSERVER=\\DESKTOP-2CFH9HQ
PROMPT=$P$G
SESSIONNAME=Console
ALLUSERSPROFILE=C:\ProgramData
=K:=K:\
PROCESSOR_ARCHITECTURE=AMD64
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
SystemDrive=C:
=ExitCode=00000000
OneDrive=C:\Users\Nikhil\OneDrive
APPDATA=C:\Users\Nikhil\AppData\Roaming
USERNAME=Nikhil
ProgramFiles(x86)=C:\Program Files (x86)
CommonProgramFiles=C:\Program Files\Common Files
Path=C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk-12.0.2\bin;C:\Program Files\Java\jdk-12.0.2\bin\google.jar
FPS_BROWSER_USER_PROFILE_STRING=Default
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
OS=Windows_NT
COMPUTERNAME=DESKTOP-2CFH9HQ
PROCESSOR_REVISION=8e09
CommonProgramW6432=C:\Program Files\Common Files
ComSpec=C:\Windows\system32\cmd.exe
ProgramData=C:\ProgramData
ProgramW6432=C:\Program Files
HOMEPATH=\Users\Nikhil
SystemRoot=C:\Windows
TEMP=C:\Users\Nikhil\AppData\Local\Temp
HOMEDRIVE=C:
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
USERPROFILE=C:\Users\Nikhil
TMP=C:\Users\Nikhil\AppData\Local\Temp
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
ProgramFiles=C:\Program Files
PUBLIC=C:\Users\Public
NUMBER_OF_PROCESSORS=4
windir=C:\Windows
=::=::\

Note: Depending on your operating system, you can receive a different result.

System.getProperty():

Java.lang is an additional option.In Java, use System.getProperty() to get the value for the key that was given as a parameter. With the help of this method, you may get information about the local system and configurations as well as the system properties. It is preferable to use System.getProperty() to retrieve the value of an environment variable if it is available in Java as a system property in order to do it in a platform-independent manner. As opposed to environment variables, which cannot be altered during runtime, properties can.

Syntax:

Name is the parameter used to define the key for which the value was required in this context.

Returns:

It will either return the key's value or null.

Implementation:

FileName: SystemGetProperty.java

Output:

user.name : Nikhil
home : null
os.name : Windows 10





Latest Courses