How do I display the environment variables?
Author: Deron Eriksson
Description: This Java example shows how to display the environment variables.
Tutorial created using: Windows XP || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
By calling the getenv() method of System, we can obtain a map containing the environment variables (ie, the keys) and their values. The EnvironmentVariablesTest class demonstrates this. A TreeMap was used so that we could view the environment variables in sorted order.
EnvironmentVariablesTest.java
package test; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; public class EnvironmentVariablesTest { public static void main(String[] args) throws Exception { MapenvMap = System.getenv(); SortedMap sortedEnvMap = new TreeMap (envMap); Set keySet = sortedEnvMap.keySet(); for (String key : keySet) { String value = envMap.get(key); System.out.println("[" + key + "] " + value); } } }
The console output of executing EnvironmentVariablesTest is shown here.
Console output
[=::] ::\ [ALLUSERSPROFILE] C:\ProgramData [APPDATA] C:\Users\Cakes\AppData\Roaming [COMPUTERNAME] YOSEMITE [CYGWIN_HOME] C:\dev\cygwin [ComSpec] C:\Windows\system32\cmd.exe [CommonProgramFiles] C:\Program Files\Common Files [FP_NO_HOST_CHECK] NO [HOMEDRIVE] C: [HOMEPATH] \Users\Cakes [JAVA_HOME] C:\dev\jdk1.6.0_04 [LOCALAPPDATA] C:\Users\Cakes\AppData\Local [LOGONSERVER] \\YOSEMITE [M2_HOME] C:\dev\apache-maven-2.0.8 [NUMBER_OF_PROCESSORS] 2 [OS] Windows_NT [OnlineServices] Online Services [PATHEXT] .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC [PCBRAND] Pavilion [PLATFORM] HPD [PROCESSOR_ARCHITECTURE] x86 [PROCESSOR_IDENTIFIER] x86 Family 15 Model 107 Stepping 1, AuthenticAMD [PROCESSOR_LEVEL] 15 [PROCESSOR_REVISION] 6b01 [PUBLIC] C:\Users\Public [Path] C:\dev\jdk1.6.0_04\bin\..\jre\bin\client;C:\dev\jdk1.6.0_04\bin\..\jre\bin;C:\dev\cygwin\bin;C:\dev\jdk1.6.0_04\bin;C:\dev\apache-maven-2.0.8\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\hp\bin\Python;c:\Program Files\Common Files\Roxio Shared\DLLShared\;c:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\ [ProgramData] C:\ProgramData [ProgramFiles] C:\Program Files [RoxioCentral] c:\Program Files\Common Files\Roxio Shared\9.0\Roxio Central33\ [SESSIONNAME] Console [SystemDrive] C: [SystemRoot] C:\Windows [TEMP] C:\Users\Cakes\AppData\Local\Temp [TMP] C:\Users\Cakes\AppData\Local\Temp [USERDOMAIN] Yosemite [USERNAME] Cakes [USERPROFILE] C:\Users\Cakes [windir] C:\Windows
No comments:
Post a Comment