Skip to main content

How to Read an Environment Variable in Java

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change between computers, such as a port, a database address or an API key, belong in environment variables. Java reads one with System.getenv("NAME"), which returns null when the variable isn't set.

Syntax​

Syntax
String <value> = System.getenv("<NAME>");
PartWhat it does
System.getenvReads a variable from the environment the program started with.
"<NAME>"The variable's name.
<value>The value as a String, or null when the variable isn't set.

The JDK 25 documentation for System.getenv: "Gets the value of the specified environment variable." It returns the value, or null if the variable is not defined.

The Example​

The example prints the value of a variable named GREETING, first with the variable set and then without it.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which reads System.getenv("GREETING") into greeting and prints GREETING is: followed by its value.

Main.java
public class Main {
public static void main(String[] args) {
String greeting = System.getenv("GREETING");
System.out.println("GREETING is: " + greeting);
}
}

Step 2: Set the Variable and Run It​

Set GREETING to Hello and run the program. Then remove the variable and run it again:

Terminal
export GREETING=Hello
java Main.java
unset GREETING
java Main.java

A zsh terminal where export GREETING=Hello and java Main.java print GREETING is: Hello, and after unset GREETING the same command prints GREETING is: null.

Output
GREETING is: Hello
GREETING is: null

With the variable set, System.getenv returned "Hello". Without it, it returned null, and string concatenation printed that as the word null. Check for null before using a value the program needs.

Names on Different Systems​

The JDK documentation warns that environment variables "can have subtly different semantics, such as case insensitivity, on different operating systems." On macOS and Linux, System.getenv("greeting") won't find GREETING; on Windows, names aren't case-sensitive.

Common Mistakes​

MistakeWhat happensDo this instead
Calling a method on the result without a null checkNullPointerException when the variable isn't setCheck for null first
Using a number from the environment directlyIt's a String, such as "3000"Convert it with Integer.parseInt
Setting the variable in one terminal and running Java in anothernull: each terminal has its own variablesSet it in the terminal that runs the program

Troubleshooting​

SymptomLikely causeFix
The output says nullThe variable isn't set in this terminal, or the name's case differsSet it as in Step 2 and match the name exactly
NumberFormatException after reading a numberThe variable holds text that isn't a number, or is missingCheck the value before parsing

FAQ​

What's the difference between getenv and getProperty?​

System.getenv reads environment variables, set outside Java. System.getProperty reads Java system properties, set with -Dname=value when starting Java.

Can I read all variables at once?​

Yes: System.getenv() without arguments returns all of them as a Map<String, String>.

Conclusion​

System.getenv("NAME") returns an environment variable as a String, or null when it isn't set. Check for null, and match the name's case on macOS and Linux.

Additional Resources​