Java Home Not Set – Causes, Examples, and Complete Fix Guide

The “JAVA_HOME Not Set” error is one of the most common issues developers encounter while installing Java or configuring development tools such as Maven, Gradle, Tomcat, IntelliJ IDEA, Eclipse, Android Studio, and VS Code. This error occurs when the operating system or a Java-based application cannot locate the Java Development Kit (JDK) installation directory.

If you’ve seen an error like:

JAVA_HOME is not set.

Please set the JAVA_HOME environment variable.

or

Error: JAVA_HOME environment variable is not defined correctly.

don’t worry. In this guide, you’ll learn what the JAVA_HOME variable is, why the error occurs, and step-by-step solutions to fix it on Windows, Linux, and macOS.

What is JAVA_HOME?

JAVA_HOME is an environment variable that stores the location of the installed Java Development Kit (JDK).

Many Java applications use this variable to locate Java tools such as:

  • java
  • javac
  • jar
  • javadoc
  • jdb

Instead of searching the entire system, applications simply read the JAVA_HOME variable.

Example:


C:\Program Files\Java\jdk-21
or
/usr/lib/jvm/jdk-21

Why Does the “JAVA_HOME Not Set” Error Occur?

This error appears when Java applications cannot determine the location of the installed JDK.

Common causes include:

  • JAVA_HOME variable is missing
  • Incorrect JAVA_HOME path
  • JDK is not installed
  • Only JRE is installed
  • PATH variable is incorrect
  • Multiple Java versions installed
  • JAVA_HOME points to the wrong directory
  • IDE configuration issues

How to Check if JAVA_HOME is Set

Before fixing the issue, verify whether JAVA_HOME is configured.

Windows

Open Command Prompt:


echo %JAVA_HOME%

Example output:

C:\Program Files\Java\jdk1-2

If nothing is displayed, JAVA_HOME has not been configured.

Linux/macOS

Open Terminal:


echo $JAVA_HOME

If the command returns an empty value, the variable is not set.

Solution 1: Install the JDK

The first step is to ensure that the Java Development Kit (JDK) is installed.

Check Java:


java -version

Check Java compiler:


javac -version

If javac is missing, you probably have only the Java Runtime Environment (JRE) installed.

Install the latest JDK and then configure JAVA_HOME.

Solution 2: Find the JDK Installation Directory

Locate your JDK installation.

Windows

Typical locations


C:\Program Files\Java\jdk-21
C:\Program Files\Java\jdk-17

Linux


/usr/lib/jvm/jdk-21
or
/usr/lib/jvm/java-17-openjdk

macOS


/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home

Use this directory for the JAVA_HOME variable.

Solution 3: Set JAVA_HOME on Windows

Follow these steps:

  1. Open System Properties.
  2. Click Advanced system settings.
  3. Select Environment Variables.
  4. Click New under System Variables.
  5. Enter:

Variable Name:


JAVA_HOME

Variable Value:


C:\Program Files\Java\jdk-21

Click OK and save the changes.

Restart Command Prompt before testing.

Solution 4: Update the PATH Variable

After setting JAVA_HOME, update the system PATH.

Add:


%JAVA_HOME%\bin

to the PATH environment variable.

Verify:


java -version
and
javac -version

Both commands should work correctly.

Solution 5: Set JAVA_HOME on Linux

Open the shell configuration file.

For Bash:


nano ~/.bashrc

Add:


export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH

Save the file.

Reload:


source ~/.bashrc

Verify:


echo $JAVA_HOME

Solution 7: Verify JAVA_HOME Points to the JDK

Many developers accidentally point JAVA_HOME to the bin folder.

Incorrect:


C:\Program Files\Java\jdk-21\bin

Correct:


C:\Program Files\Java\jdk-21

JAVA_HOME should always point to the JDK root directory, not its bin subfolder.

Solution 8: Check for Multiple Java Versions

If multiple JDK versions are installed, JAVA_HOME may reference the wrong one.

Example:


jdk-8
jdk-17
jdk-21

Verify:


java -version
javac -version

Ensure both commands use the same JDK version.

Solution 9: Configure JAVA_HOME in IDEs

Some IDEs maintain their own Java configuration.

IntelliJ IDEA

  • Open Project Structure
  • Select Project SDK
  • Choose the correct JDK

Eclipse

  • Preferences
  • Java
  • Installed JREs
  • Select the correct JDK

VS Code

Configure the Java Runtime in the Java Extension settings.

Keeping the IDE and system Java versions aligned prevents configuration issues.

Solution 10: Configure Build Tools

Many build tools require JAVA_HOME.

Maven

Verify:


mvn -version

Output should display:

  • Java version
  • JAVA_HOME location

Gradle

Check:

gradle -version

If JAVA_HOME is incorrect, these tools may fail to start.

Solution 11: Restart Your Terminal or Computer

After modifying environment variables:

  • Close Command Prompt or Terminal.
  • Open a new session.
  • In some cases, restart the computer.

Environment variable changes are not always visible to already running applications.

Solution 12: Verify the Configuration

Run:


echo %JAVA_HOME%

Windows

or


echo $JAVA_HOME

Linux/macOS

Then verify:


java -version
javac -version

Expected example:


Expected example:

java version “21”

javac 21

If all commands return the expected output, your configuration is correct.

Real-World Example

Suppose you run:


mvn clean install

Output

JAVA_HOME is not set correctly.

You configure:


JAVA_HOME=C:\Program Files\Java\jdk-21

Update PATH:


%JAVA_HOME%\bin

Restart the terminal and execute:


mvn clean install

The build now runs successfully because Maven can locate the JDK.

Common Scenarios That Cause “JAVA_HOME Not Set”

Scenario Solution
JDK not installed Install the Java Development Kit
JAVA_HOME missing Create the environment variable
JAVA_HOME points to bin Point it to the JDK root directory
PATH missing Java Add %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Linux/macOS)
Multiple JDK versions Configure the correct JDK
IDE using wrong JDK onfigure the Java toolchain correctly
IDE using the wrong JDK Update IDE Java settings
Maven cannot find Java Verify JAVA_HOME and restart the terminal
Gradle fails to start Check JAVA_HOME configuration</td
Old terminal session Restart the terminal or computer

Best Practices to Avoid JAVA_HOME Errors

Follow these recommendations:

  • Install the JDK instead of only the JRE.
  • Keep JAVA_HOME pointing to the JDK root folder.
  • Add $JAVA_HOME/bin or %JAVA_HOME%\bin to the PATH variable.
  • Avoid hardcoding Java paths in scripts whenever possible.
  • Use the same Java version across development, testing, and production environments.
  • Remove unused or outdated Java installations to reduce confusion.
  • Verify java -version and javac -version after installing a new JDK.
  • Restart terminals and IDEs after changing environment variables.
  • Keep Maven, Gradle, and IDE settings synchronized with the installed JDK.
  • Document the required Java version for your project.

Frequently Asked Questions (FAQs)

What is JAVA_HOME?

JAVA_HOME is an environment variable that stores the installation path of the Java Development Kit (JDK). Many Java tools and frameworks use it to locate the Java compiler and runtime.

Why do I get the “JAVA_HOME Not Set” error?

This error usually occurs because:

  • The JDK is not installed.
  • JAVA_HOME is missing.
  • JAVA_HOME points to the wrong directory.
  • The PATH variable is not configured correctly.
  • The application is using a different Java installation than expected.

Should JAVA_HOME point to the JDK or the bin folder?

JAVA_HOME should point to the JDK installation directory, not the bin folder.

Correct:


C:\Program Files\Java\jdk-21

Incorrect:


C:\Program Files\Java\jdk-21\bin

How can I verify that JAVA_HOME is configured correctly?

Run:


echo %JAVA_HOME%

(Windows)

or


echo $JAVA_HOME

(Linux/macOS)

Then verify:

java -version

and

javac -version

If all commands display the expected information, your Java environment is configured correctly.

Conclusion

The “JAVA_HOME Not Set” error is one of the most common Java configuration problems, but it is also one of the easiest to fix. In most cases, the issue is caused by a missing or incorrect JAVA_HOME environment variable, an improperly configured PATH, or the absence of the JDK. By installing the correct JDK, setting JAVA_HOME to the JDK root directory, updating the PATH variable, and ensuring your IDE and build tools use the same Java version, you can eliminate this error and create a stable Java development environment for applications, frameworks, and build tools.

Related Java Tutorials