w3resource

Java: Get the value of the environment variable PATH, TEMP, USERNAME


Environment Variables

Write a Java program to get the value of environment variables PATH, TEMP, USERNAME.

Sample Solution:

Java Code:

import java.lang.*;

public class Exercise90 {
    public static void main(String[] args) {
        // Display the value of the specified environment variable "PATH"
        System.out.println("\nEnvironment variable PATH: ");
        System.out.println(System.getenv("PATH"));

        // Display the value of the specified environment variable "TEMP"
        System.out.println("\nEnvironment variable TEMP: ");
        System.out.println(System.getenv("TEMP"));

        // Display the value of the specified environment variable "USERNAME"
        System.out.println("\nEnvironment variable USERNAME: ");
        System.out.println(System.getenv("USERNAME"));
    }
}

Sample Output:

Environment variable PATH:                                             
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
:/usr/local/games                                                      
                                                                       
Environment variable TEMP:                                             
null                                                                   
                                                                       
Environment variable USERNAME:                                         
null

Flowchart:

Flowchart: Java exercises: Get the value of the environment variable PATH, TEMP, USERNAME


For more Practice: Solve these Related Problems:

  • Write a Java program to list all environment variables and sort them alphabetically.
  • Write a Java program to check if a specific environment variable exists and display a custom message if it doesn’t.
  • Write a Java program to modify an environment variable value within the JVM and print the updated value.
  • Write a Java program to compare environment variables between two different operating systems.

Go to:


PREV : Check Security Manager.
NEXT : Code Execution Time in Nanoseconds.


Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.