![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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