Java How to – Get Environment Variables

Problem Statement

You need to programmatically get the values of the environment variables from your java program.

Solution

Use java.lang.System.getenv method to get the environment variables from your java program from Java JDK 1.5 and higher version. You can pass the “PATH” as parameter to the method to retreive the path from the environment variable.

public class Main {

public static void main(String[] args) {

    System.out.println("Environment Path = " + System.getenv("PATH"));

}

}

When no arguments are passed, it returns all the environment variables.