Skip to content
javahacks.dev
DevOps

Fix the JAVA HOME Is Not Defined Correctly Error in Maven

Fix Maven's JAVA_HOME error by pointing the variable at a JDK directory, not at bin or java, on macOS, Linux and Windows, and verify it with mvn -v.

6 min read
Summarize in AI
Flat illustration of a dependency tree and build phases, with the Apache Maven, Docker and Jenkins logos.

You run mvn clean package and Maven stops before compiling anything:

Text
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/lib/jvm/java-25-openjdk/bin/java

Windows prints the same idea in its own words: The JAVA_HOME environment variable is not defined correctly. This environment variable is needed to run this program. Maven's launcher script builds the path $JAVA_HOME/bin/java and checks that it exists. If it does not, you get this. The variable is set, it just points at the wrong thing. This post shows what the value must be, how to fix it on macOS, Linux and Windows, how to verify with mvn -v, and why it can work in your terminal and fail in an IDE or in CI. The examples use Maven 3.9 and JDK 25.

What the value must be

JAVA_HOME is the JDK installation directory, the one that contains a bin folder with java inside it. It is not the bin folder, not the java executable, and not a directory one level too high or low.

ValueCorrect?Why
/usr/lib/jvm/java-25-openjdkYesbin/java exists under it
/usr/lib/jvm/java-25-openjdk/binNoMaven looks for bin/bin/java
/usr/lib/jvm/java-25-openjdk/bin/javaNoThat is a file, not a home
/usr/lib/jvm/java-25-openjdk/Usually worksA trailing slash is accepted by newer scripts but is fragile
The Windows install path wrapped in double quotesNoThe quotes become part of the path
/usr/lib/jvmNoThe JDKs are children of it

The fastest self-test is one command. If this fails, the value is wrong:

Shell
"$JAVA_HOME/bin/java" -version

Point it at what that prints and nothing else. The variable is not about the java that happens to be first on your PATH; Maven prefers JAVA_HOME when it is set, which is why a broken value breaks Maven even though java -version works fine.

macOS and Linux

First find out where your JDK actually lives. On macOS, the system helper knows:

Shell
/usr/libexec/java_home -V
Text
Matching Java Virtual Machines (1):
    25.0.1 (arm64) "Eclipse Adoptium" - "OpenJDK 25.0.1" /Library/Java/JavaVirtualMachines/temurin-25.jdk/Contents/Home

The path at the end is the value, including Contents/Home. Set it for the current shell, then persist it:

Shell
export JAVA_HOME=$(/usr/libexec/java_home -v 25)
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 25)' >> ~/.zshrc

Letting java_home compute it means a JDK update does not leave a stale path behind. On Linux there is no such helper, so resolve the real location of the java binary and strip two path components:

Shell
readlink -f "$(which java)"
Text
/usr/lib/jvm/java-25-openjdk-amd64/bin/java

Remove /bin/java from the end, and what is left is the home:

Shell
export JAVA_HOME=/usr/lib/jvm/java-25-openjdk-amd64
echo 'export JAVA_HOME=/usr/lib/jvm/java-25-openjdk-amd64' >> ~/.bashrc

Do not use the symlink /usr/bin/java as a starting point and chop it. It points through /etc/alternatives, and readlink -f follows the whole chain. If you installed from an archive, the home is the extracted directory itself. Distribution packages and the Temurin downloads from Adoptium both install to a predictable directory per version.

Windows

Open System Properties, then Environment Variables, and create or edit JAVA_HOME with the install directory, with no quotes and no trailing semicolon:

Text
C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot

A change in that dialog is invisible to windows that are already open. Close and reopen the terminal, and check:

bat
echo %JAVA_HOME%
"%JAVA_HOME%\bin\java" -version

From PowerShell, set it for the user permanently with:

powershell
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot", "User")

The same pitfalls apply. A value ending in the bin folder is the most common Windows mistake, because the installer's PATH entry ends that way and people copy it.

Verify with mvn -v

The proof is that Maven starts and reports the Java it found:

Shell
mvn -v
Text
Apache Maven 3.9.11
Maven home: /opt/maven
Java version: 25.0.1, vendor: Eclipse Adoptium, runtime: /usr/lib/jvm/java-25-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.8.0", arch: "amd64", family: "unix"

Read the runtime: line. It must be the JDK you meant. If it names a different version than your project needs, you have fixed the error and found the next one, a compiler release mismatch. That one looks like the failures in maven-compiler-plugin failed to execute goal or, at runtime, UnsupportedClassVersionError.

Also confirm you have a JDK and not only a JRE. Maven needs javac:

Shell
"$JAVA_HOME/bin/javac" -version

A JRE-only home has bin/java but no bin/javac, so the error is different and shows later, as No compiler is provided in this environment.

When it works in a terminal and not elsewhere

The shell where you fixed it is only one place a process can start from. These are the usual others.

An IDE. IntelliJ IDEA runs Maven with its own bundled Maven and its own JDK setting. Open Settings, Build Tools, Maven, and look at "Runner" and "JRE". If "Use JAVA_HOME" is selected, it reads the environment the IDE was launched with, which on macOS means launched from the Dock, not from your shell profile. Launch it from a terminal once, or set the JDK explicitly.

A cron job or a service. These run with a minimal environment and do not source your ~/.bashrc. Set JAVA_HOME inside the script or unit file.

A CI runner. Use the platform's setup action and let it set the variable. In GitHub Actions:

.github/workflows/build.ymlYAML
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-java@v4
    with:
      distribution: temurin
      java-version: "25"
      cache: maven
  - run: mvn -B verify

setup-java exports JAVA_HOME for every later step. A full pipeline built on this is in setting up Java CI with GitHub Actions.

**sudo.** sudo mvn resets the environment by default, so a variable exported in your shell disappears. Use sudo -E or, better, avoid running Maven as root.

The Maven wrapper

If the repository ships mvnw, the wrapper script has the same check. It downloads Maven on first use but still needs a valid Java:

Shell
./mvnw -v

The error text is the same, and so is the fix. The wrapper's advantage is that the Maven version is pinned by the repository, so only JAVA_HOME is yours to get right. The upstream install instructions describe the same requirement.

Where this does not apply

If the message names a path that exists and runs, the cause may be architecture: an x86 JDK on an Apple Silicon machine under a shell that cannot execute it, or a 32-bit JDK where a 64-bit one is expected. Run file "$JAVA_HOME/bin/java" to see. If mvn itself is not found, JAVA_HOME is not your problem; fix the PATH and the Maven home instead. And if the project needs several JDKs, a version manager such as SDKMAN! sets JAVA_HOME per shell, so do not hardcode the variable in a profile that fights it.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.