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)


Page:    1 2 >

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 {

		Map<String, String> envMap = System.getenv();
		SortedMap<String, String> sortedEnvMap = new TreeMap<String, String>(envMap);
		Set<String> keySet = sortedEnvMap.keySet();
		for (String key : keySet) {
			String value = envMap.get(key);
			System.out.println("[" + key + "] " + value);
		}

	}

}

(Continued on page 2)

Page:    1 2 >