Close

Java - System.getenv() Examples

Java Java API 


Class:

java.lang.System

java.lang.Objectjava.lang.Objectjava.lang.Systemjava.lang.SystemLogicBig

Methods:

public static String getenv(String name)

Returns the value of the specified environment variable. An environment variable is a system-dependent external named value.

The difference between system properties and environment variables is : properties are set for the currently running Java process, they are usually set as -Dname=value while running a Java program. Whereas, Environmental variables are set on O.S. level e.g. path variable, JAVA_HOME etc.



public static Map<String,String> getenv()

Returns an unmodifiable string map view of the current system environment. The environment is a system-dependent mapping from names to values which is passed from parent to child processes.


Examples


package com.logicbig.example.system;

import java.util.Map;

public class GetenvExample {

public static void main(String... args) {
Map<String, String> map = System.getenv();
map.entrySet().forEach(System.out::println);
}
}

Output

configsetroot=C:\WINDOWS\ConfigSetRoot
USERDOMAIN_ROAMINGPROFILE=JOEMSI
PROCESSOR_LEVEL=6
FP_NO_HOST_CHECK=NO
SESSIONNAME=Console
ALLUSERSPROFILE=C:\ProgramData
PROCESSOR_ARCHITECTURE=AMD64
PSModulePath=;D:\Google\Cloud SDK\google-cloud-sdk\platform\PowerShell
SystemDrive=C:
USERNAME=Joe
JDK_HOME=C:\Program Files\Java\jdk1.8.0_111
ProgramFiles(x86)=C:\Program Files (x86)
WDIR=D:\
FPS_BROWSER_USER_PROFILE_STRING=Default
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
DART_SDK=d:\Dart\dart-sdk
CLASSWORLDS_JAR="D:\apache-maven-3.3.9\boot\plexus-classworlds-2.5.2.jar"
ProgramData=C:\ProgramData
ProgramW6432=C:\Program Files
HOMEPATH=\Users\Joe
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 71 Stepping 1, GenuineIntel
M2_HOME=D:\apache-maven-3.3.9
MAVEN_JAVA_EXE="C:\Program Files\Java\jdk1.8.0_111\bin\java.exe"
ProgramFiles=C:\Program Files
PUBLIC=C:\Users\Public
windir=C:\WINDOWS
LOCALAPPDATA=C:\Users\Joe\AppData\Local
USERDOMAIN=JOEMSI
FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
LOGONSERVER=\\JOEMSI
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_111
PROMPT=$P$G
ERROR_CODE=0
EXEC_DIR=D:\LogicBig\example-projects\java\java-core-api-snippets\java-lang
OneDrive=C:\Users\Joe\OneDrive
=C:=C:\Users\Joe
APPDATA=C:\Users\Joe\AppData\Roaming
MAVEN_CMD_LINE_ARGS=compile -Dexec.mainClass="com.logicbig.example.system.GetenvExample" exec:java -Dexec.cleanupDaemonThreads=false
VBOX_MSI_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\
CommonProgramFiles=C:\Program Files\Common Files
Path=C:\Program Files (x86)\ActiveState Komodo Edit 10\;C:\ProgramData\Oracle\Java\javapath;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)\Windows Live\Shared;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Java\jdk1.8.0_65\bin;D:\apache-maven-3.3.9\bin;C:\Program Files\WinHTTrack;d:\tools;d:\Dart\dart-sdk\bin;D:\php-5.6.26-Win32-VC11-x86;C:\Python\Scripts;C:\Program Files\Git\cmd;C:\Program Files (x86)\Skype\Phone\;D:\spring\spring-1.5.2.RELEASE\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PuTTY\;C:\Python27;C:\Python27\Scripts;C:\Users\Joe\AppData\Local\Microsoft\WindowsApps;C:\Users\Joe\AppData\Local\atom\bin;C:\Program Files\Heroku\bin;D:\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\
OS=Windows_NT
COMPUTERNAME=JOEMSI
MAVEN_PROJECTBASEDIR=D:\LogicBig\example-projects\java\java-core-api-snippets\java-lang
PROCESSOR_REVISION=4701
CommonProgramW6432=C:\Program Files\Common Files
ComSpec=C:\WINDOWS\system32\cmd.exe
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
=D:=D:\LogicBig\example-projects\java\java-core-api-snippets\java-lang
CLOUDSDK_APP_USE_GSUTIL=1
SystemRoot=C:\WINDOWS
TEMP=C:\Users\Joe\AppData\Local\Temp
HOMEDRIVE=C:
USERPROFILE=C:\Users\Joe
TMP=C:\Users\Joe\AppData\Local\Temp
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
NUMBER_OF_PROCESSORS=8
HOME=C:\Users\Joe




package com.logicbig.example.system;

public class GetenvExample2 {

public static void main(String... args) {
//not case sensitive
String homePath = System.getenv("homePath");
System.out.println(homePath);
}
}

Output

\Users\Joe




See Also