Home Java Beginners Get Environment Variable Java

 
 

Share on Google+Share on Google+

Get Environment Variable Java

Advertisement
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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 6, 2007

Related Tutorials

Discuss: Get Environment Variable Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
rajasekhar burepalli
January 7, 2013
Print Path variable data using Java?

import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; public class Credentials { public static void main(String k[]) { Map m=(Map)System.getenv(); Set s=m.entrySet(); Iterator i=s.iterator(); while(i.hasNext()) { Map.Entry me=(Map.Entry)i.next(); System.out.print(me.getKey()); StringTokenizer st=new StringTokenizer(me.getValue().toString()); while(st.hasMoreTokens()) { System.out.println( "\t\t\t"+st.nextToken(";")); } } } }
DMCA.com