• home
  • forum
  • my
  • kt
  • download
  • Get Environment Variables in Java

    Author: 2007-08-03 14:39:39 From:

    In Java 1.5, Sun had decided to "undeprecate" the getEnv() method that provides the functionality to get the value of an environment variable. In addition, Sun has added a new method that allows you to discover all of the environment variables that are defined. This tutorial walks you through using both the old and new methods.

    To get the value of a single environment variable, you simply need to call the getEnv("environment variable") method providing the environment variable name that you want to evaluate. The following is a simple example:

    String variable = System.getenv("WINDIR");
    System.out.println(variable);

    In Java 1.5, a new method was introduced to allow you to discover all of the environment variables defined in the system. The following example uses the new method to print all of the environment variables and their values:

    Map variables = System.getenv();
    Set variableNames = variables.keySet();
    Iterator nameIterator = variableNames.iterator();

    for (int index = 0; index < variableNames.size(); index++)
    {
         String name = (String) nameIterator.next();
         String value = (String) variables.get(name);
         System.out.println(name + "=" + value);
    }

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Applet Building (2)
      Application Building (3)
      Communication (1)
      Database Related (8)
      Development (12)
      EJB (14)
      Game Programming (2)
      General Java (38)
      Javabeans (4)
      JSP and Servlets (8)
      Miscellaneous (23)
      Networking (1)
      Security (2)
      Swing (13)
      WAP and WML (1)
      XML and Java (0)

    New

    Hot