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) 2017 by Dr. Herong Yang, herongyang.com
 */
class SystemEnvironmentVariable {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      
      // copying all environment variables into a map
      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 7 system with JDK 1.8.0, I got this result:

C:\herong\jvm>java SystemEnvironmentVariable

Envirionment Variables:
   configsetroot = C:\windows\ConfigSetRoot
   PROCESSOR_LEVEL = 6
   FP_NO_HOST_CHECK = NO
   SESSIONNAME = Console
   ALLUSERSPROFILE = C:\ProgramData
   TVTCOMMON = C:\Program Files (x86)\Common Files\Lenovo
   PROCESSOR_ARCHITECTURE = x86
   PSModulePath = C:\windows\system32\WindowsPowerShell\v1.0\Modules\;...
   SystemDrive = C:
   =ExitCode = 00000000
   COMMPath = C:\Program Files\Lenovo\Communications Utility
   TVT = C:\Program Files (x86)\Lenovo
   SWSHARE = C:\SWSHARE
   USERNAME = herong
   USERDNSDOMAIN = ENTERPRISE.CMGI.COM
   ProgramFiles(x86) = C:\Program Files (x86)
   PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
   PROCESSOR_ARCHITEW6432 = AMD64
   ProgramData = C:\ProgramData
   ProgramW6432 = C:\Program Files
   HOMEPATH = \Users\herong
   PROCESSOR_IDENTIFIER = Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
   ProgramFiles = C:\Program Files (x86)
   PUBLIC = C:\Users\Public
   windir = C:\windows
   =:: = ::\
   LOCALAPPDATA = C:\Users\herong\AppData\Local
   USERDOMAIN = LOCAL
   LOGONSERVER = \\HOME
   ...

No surprises in the output.

Last update: 2017.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version