JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How To Display Environment Variables In Java

January 31, 2014 by Krishna Srinivasan Leave a Comment

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

[code lang=”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);
}
}
}
[/code]

Output…

[code]
[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
[/code]

Filed Under: Java Tagged With: Java System Programming

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved