Java FAQ: How do I access/read system properties and environment variables?
Answer: With Java you actually read system environment variables and system properties using two different method calls, although you access them in more or less the same way, as they are both stored in a Map. The sample Java code shown below shows how to read these two different sets of properties using Java 5 syntax:
package com.devdaily.javasamples; import java.util.Map; import java.util.Properties; /** * Demonstrates how to read/access the system environment variables * and system properties. * Created by Alvin Alexander, http://devdaily.com. */ public class EnvironmentVariableExample { public static void main(String[] args) { // get the environment variables System.out.println("Environment Variables"); Map
As you can see from the output of this program, the system environment variables and properties are two different maps. First, here are the environment variables:
Environment Variables SECURITYSESSIONID = 2790d870 USER = al HOME = /Users/al APP_ICON_22269 = ../Resources/Eclipse.icns JAVA_JVM_VERSION = 1.5.0 __CF_USER_TEXT_ENCODING = 0x1F5:0:0 PATH = /usr/bin:/bin:/usr/sbin:/sbin SHELL = /bin/bash JAVA_MAIN_CLASS_22331 = com.devdaily.javasamples.EnvironmentVariableExample
Next, here is a much larger list of the system properties:
System Properties java.runtime.name = Java(TM) 2 Runtime Environment, Standard Edition sun.boot.library.path = /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Libraries java.vm.version = 1.5.0_07-87 awt.nativeDoubleBuffering = true gopherProxySet = false java.vm.vendor = "Apple Computer, Inc." java.vendor.url = http://apple.com/ path.separator = : java.vm.name = Java HotSpot(TM) Client VM file.encoding.pkg = sun.io user.country = US sun.os.patch.level = unknown java.vm.specification.name = Java Virtual Machine Specification user.dir = /Users/al/JavaTests java.runtime.version = 1.5.0_07-164 java.awt.graphicsenv = apple.awt.CGraphicsEnvironment java.endorsed.dirs = /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/endorsed os.arch = i386 java.io.tmpdir = /tmp line.separator = java.vm.specification.vendor = Sun Microsystems Inc. os.name = Mac OS X sun.jnu.encoding = MacRoman java.library.path = .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java java.specification.name = Java Platform API Specification java.class.version = 49.0 sun.management.compiler = HotSpot Client Compiler os.version = 10.4.10 user.home = /Users/al user.timezone = java.awt.printerjob = apple.awt.CPrinterJob file.encoding = MacRoman java.specification.version = 1.5 java.class.path = /Users/al/JavaTests/bin:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibility/14compatibility.jar user.name = al java.vm.specification.version = 1.0 java.home = /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home sun.arch.data.model = 32 user.language = en java.specification.vendor = Sun Microsystems Inc. awt.toolkit = apple.awt.CToolkit java.vm.info = mixed mode, sharing java.version = 1.5.0_07 java.ext.dirs = /Library/Java/Extensions:/System/Library/Java/Extensions:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext sun.boot.class.path = /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/ui.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/laf.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/sunrsasign.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jsse.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jce.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/charsets.jar java.vendor = Apple Computer, Inc. file.separator = / java.vendor.url.bug = http://developer.apple.com/java/ sun.io.unicode.encoding = UnicodeLittle sun.cpu.endian = little mrj.version = 1040.1.5.0_07-164 sun.cpu.isalist =
As you can probably tell from that output, I'm using the Java 5 platform on a Mac OS X workstation running OS X version 10.4.10 when running this program.