Java.lang.System.getenv() Method



Description

The java.lang.System.getenv(String name) method gets the value of the specified environment variable. An environment variable is a system-dependent external named value.

Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).

Declaration

Following is the declaration for java.lang.System.getenv() method

public static String getenv(String name)

Parameters

name − This is the name of the environment variable.

Return Value

This method returns the string value of the variable, or null if the variable is not defined in the system environment.

Exception

  • NullPointerException − if name is null.

  • SecurityException − if a security manager exists and its checkPermission method doesn't allow access to the process environment.

Example

The following example shows the usage of java.lang.System.getenv() method.

package com.tutorialspoint;

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) throws Exception {

      // gets the value of the specified environment variable "PATH"
      System.out.println("System.getenv("PATH") = ");
      System.out.println(System.getenv("PATH"));

      // gets the value of the specified environment variable "TEMP"
      System.out.print("System.getenv("TEMP") = ");
      System.out.println(System.getenv("TEMP"));

      // gets the value of the specified environment variable "USERNAME"
      System.out.print("System.getenv("USERNAME") = ");
      System.out.println(System.getenv("USERNAME"));
   }
} 

Let us compile and run the above program, this will produce the following result −

System.getenv("PATH") = 
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
System.getenv("TEMP") = C:\DOCUME~1\AMIT~1.AMI\LOCALS~1\Temp
System.getenv("USERNAME") = amroodAdmin
java_lang_system.htm
Advertisements