Get Environment Variable Java

In this example we are getting environment variable. To get the environment variable we use getenv() method.

Get Environment Variable Java

In this example we are getting environment variable. To get the environment variable we use getenv() method.

Get Environment Variable Java

Get Environment Variable Java

     

In this example we are getting environment variable. To get the environment variable we use getenv() method.

Map is an interface that stores the objects in the form of key/value pairs. Map interface does not allow duplicate keys. Set is also an interface that  extends the Collection interface and like the Map it also does not allow the duplicate elements.

Methods used:

getenv(): The method getenv() is an static method of the System class and gets the information about environment variable.

get(key): This method is used to get the key values from a map class.

The code of the program is given below:

import java.util.Map;
import java.util.Set;
import java.util.Iterator;
 
public class EnvironmentInformation{
  public static void main(String[] args){
  Map map = System.getenv();
  Set keys = map.keySet();
  Iterator iterator = keys.iterator();
  System.out.println("Variable Name \t Variable Values");
  while (iterator.hasNext()){
  String key = (Stringiterator.next();
  String value = (Stringmap.get(key);
  System.out.println(key + " " + value);
  }
  }
}

The output of the program is given below:

C:\rajesh\kodejava>javac EnvironmentInformation.java
C:\rajesh\kodejava>java EnvironmentInformation
Variable Name    Variable Values
USERPROFILE     C:\Documents and Settings\Administrator
PATHEXT     .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
=ExitCode     00000000
TEMP     C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
SystemDrive     C:
ProgramFiles     C:\Program Files
Path     C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;
C:\jdk1.6.0\bi
HOMEDRIVE     C:
PROCESSOR_REVISION     0409
=C:     C:\rajesh\kodejava
USERDOMAIN     DEMO1
ALLUSERSPROFILE     C:\Documents and Settings\All Users
PROCESSOR_IDENTIFIER     x86 Family 15 Model 4 Stepping 9,
 GenuineIntel
Os2LibPath     C:\WINNT\system32\os2\dll;
TMP     C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
LOGONSERVER     \\DEMO1
CommonProgramFiles     C:\Program Files\Common Files
=::     ::\
PROCESSOR_ARCHITECTURE     x86
OS     Windows_NT
HOMEPATH     \
PROMPT     $P$G
PROCESSOR_LEVEL     15
classpath     C:\apache-tomcat-5.5.23\common\lib;
COMPUTERNAME     DEMO1
windir     C:\WINNT
SystemRoot     C:\WINNT
NUMBER_OF_PROCESSORS     2
USERNAME     Administrator
ComSpec     C:\WINNT\system32\cmd.exe
APPDATA     C:\Documents and Settings\Administratorion Data

Download this example.