JAVA_HOME Explained
What is JAVA_HOME, why Maven and Gradle need it, and how to set it on Windows without mistakes. Covers JDK vs JRE, multi-version setups, and common errors.
JAVA_HOME is an environment variable that tells build tools and IDEs where your Java Development Kit is installed. Misconfiguring it is the single most common reason Maven and Gradle builds fail on Windows.
What Is JAVA_HOME?
JAVA_HOME is a Windows environment variable that stores the path to your Java Development Kit (JDK) installation directory. When a build tool like Apache Maven or Gradle starts up, one of the first things it does is read the JAVA_HOME variable to locate the JDK — specifically to find the Java compiler (javac.exe), the jar tool, and the libraries needed to compile your project.
A correctly set JAVA_HOME looks like this:
JAVA_HOME=C:\Program Files\Java\jdk-21.0.10
Common Mistake: Do NOT point JAVA_HOME to the bin subfolder. It should point to the root of the JDK directory.
Why Do Maven and Gradle Need It?
Maven needs more than just java.exe. It needs access to the JDK's entire directory structure to find the compiler, the archive tool, and required libraries. Without JAVA_HOME, Maven cannot reliably "walk" the file system to find these tools based just on a PATH entry.
JAVA_HOME vs. PATH
| Variable | Purpose |
|---|---|
| JAVA_HOME | Points to the JDK root. Used by build tools. |
| PATH | Makes commands like "java" runnable globally. |
How to Set It (Quick Steps)
- Open System Properties (search for "Env" in Start).
- Click Environment Variables.
- Under System variables, click New.
- Set Name:
JAVA_HOME. - Set Value: Path to your JDK (e.g.,
C:\Program Files\Java\jdk-21). - Edit the Path variable and add
%JAVA_HOME%\bin.