Home Java Beginners Get Environment Variable Java



Get Environment Variable Java
Posted on: July 6, 2007 at 12:00 AM
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.

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.

Related Tags for Get Environment Variable Java:
cormforminterfaceairobjectioobjectsmapcollectionkeyvalueintaikeyssetelementelementslikefortostoreextendexteitextendsnotlsliceinnopairrmmntcajaceesemendallmeobjcatdotorssocolatkisirhallcollectpairsduandcollectxtvassthstapaluhatctsfaceendsendslsojeicaicaplndonolo


More Tutorials from this section

Ask Questions?    Discuss: Get Environment Variable Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.