Java System getenv() Method



Description

The Java 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: Getting System Path

The following example shows the usage of Java System getenv() method. In this program, we've passed PATH to System.getEnv() method to get the current System path variable value and printed the result.

package com.tutorialspoint;

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"));
   }
} 

Output

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

System.getenv('PATH') = 
C:\Program Files\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;...

Example: Getting System Username

The following example shows the usage of Java System getenv() method. In this program, we've passed USERNAME to System.getEnv() method to get the current System username value and printed the result.

package com.tutorialspoint;

public class SystemDemo {

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

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

Output

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

System.getenv('USERNAME') = Tutorialspoint

Example: Getting Temp Directory Path

The following example shows the usage of Java System getenv() method. In this program, we've passed PATH to System.getEnv() method to get the current System path variable value and printed the result.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) throws Exception {
   
      // gets the value of the specified environment variable "TEMP"
      System.out.print("System.getenv('TEMP') = ");
      System.out.println(System.getenv("TEMP"));

   }
} 

Output

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

System.getenv('TEMP') = C:\Users\TUTORI~1\AppData\Local\Temp
java_lang_system.htm
Advertisements