JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Accessing System Environment Variables

This section provides a tutorial example on how to access environment variables defined in the operating system using the System.getenv() method.

If you want to access environment variables defined in the operating system, you can use the System.getenv() method, which returns all environment variables as a java.util.Map object.

Here is tutorial example program to list all environment variables defined in the operating system:

/**
 * SystemEnvironmentVariable.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class SystemEnvironmentVariable {
   public static void main(String[] a) {

      // copying all environment variables into a map
      java.util.Map envs = System.getenv();
      java.util.Map envs = System.getenv();
      java.util.Set keys = envs.keySet();
      java.util.Iterator i = keys.iterator();

      out.println("Envirionment Variables:");
      while (i.hasNext()) {
         String k = (String) i.next();
         String v = (String) envs.get(k);
         out.println("   "+k+" = "+v);
      }
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java SystemEnvironmentVariable
Envirionment Variables:
   USERPROFILE = C:\Documents and Settings\herong
   PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1
   JAVA_HOME = C:\local\jdk
   =ExitCode = 00000000
   TEMP = C:\Temp
   SystemDrive = C:
   ProgramFiles = C:\Program Files
   Path = C:\WINDOWS\system32;C:\WINDOWS;
   HOMEDRIVE = C:
   PROCESSOR_REVISION = 0f06
   CLIENTNAME = Console
   SESSIONNAME = Console
   PROCESSOR_ARCHITECTURE = x86
   FP_NO_HOST_CHECK = NO
   OS = Windows_NT
   PROMPT = $P$G
   PROCESSOR_LEVEL = 6
   windir = C:\WINDOWS
   SystemRoot = C:\WINDOWS
   NUMBER_OF_PROCESSORS = 2
   ...

No surprises in the output.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

java.lang.System Class - The Operating System

 What Is java.lang.System?

 Standard Input, Output, and Error Streams

 Current Time in Milliseconds and Nanoseconds

Accessing System Environment Variables

 Getting and Adding System Properties

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Accessing System Environment Variables