How To Display Environment Variables In Java

If you call getenv() method in the class System, it will return the map containing all the environment variables configured in your system. If you just iterate the map, it will display all the details. This example shows the simple program to print the environment variables. I have executed this example in the Ubuntu Linux OS, but it will work in all the platform. I have used TreeMap to sort the order of the listing.

EnvironmentVariablesExample.java

package javabeat.net.core;

import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class EnvironmentVariablesExample {
	public static void main(String[] args) {
		Map<String, String> environmentMap = System.getenv();
		SortedMap<String, String> sortedEnvMap = new TreeMap<String, String>(environmentMap);
		Set<String> keySet = sortedEnvMap.keySet();
		for (String key : keySet) {
			String value = environmentMap.get(key);
			System.out.println("[" + key + "] " + value);
		}
	}
}

Output…

[COMPIZ_CONFIG_PROFILE] ubuntu
[DBUS_SESSION_BUS_ADDRESS] unix:abstract=/tmp/dbus-ihV3MTXDtR,guid=9dd6a750057413f91d1e961d00000093
[DEFAULTS_PATH] /usr/share/gconf/ubuntu.default.path
[DESKTOP_SESSION] ubuntu
[DISPLAY] :0.0
[GDMSESSION] ubuntu
[GNOME_DESKTOP_SESSION_ID] this-is-deprecated
[GNOME_KEYRING_CONTROL] /tmp/keyring-x4RXST
[GNOME_KEYRING_PID] 1941
[GPG_AGENT_INFO] /tmp/keyring-x4RXST/gpg:0:1
[GTK_MODULES] canberra-gtk-module:canberra-gtk-module
[HOME] /home/krishna
[LANG] en_IN
[LANGUAGE] en_IN:en
[LD_LIBRARY_PATH] /usr/lib/jvm/java-6-openjdk-i386/jre/lib/i386/server:/usr/lib/jvm/java-6-openjdk-i386/jre/lib/i386:/usr/lib/jvm/java-6-openjdk-i386/jre/../lib/i386:/usr/lib/jvm/java-6-openjdk-i386/jre/lib/i386/client:/usr/lib/jvm/java-6-openjdk-i386/jre/lib/i386:
[LOGNAME] krishna
[MANDATORY_PATH] /usr/share/gconf/ubuntu.mandatory.path
[PATH] /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
[PWD] /home/krishna
[SESSION_MANAGER] local/krishna-desktop:@/tmp/.ICE-unix/1952,unix/krishna-desktop:/tmp/.ICE-unix/1952
[SHELL] /bin/bash
[SSH_AGENT_PID] 1990
[SSH_AUTH_SOCK] /tmp/keyring-x4RXST/ssh
[UBUNTU_MENUPROXY] libappmenu.so
[USER] krishna
[XAUTHORITY] /home/krishna/.Xauthority
[XDG_CONFIG_DIRS] /etc/xdg/xdg-ubuntu:/etc/xdg
[XDG_CURRENT_DESKTOP] Unity
[XDG_DATA_DIRS] /usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/
[XDG_SEAT_PATH] /org/freedesktop/DisplayManager/Seat0
[XDG_SESSION_COOKIE] 668183e0288a5cca221ebab100000007-1391125112.629260-1060816248
[XDG_SESSION_PATH] /org/freedesktop/DisplayManager/Session0

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. nice… for more java examples visit http://java2novice.com site

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better