Of course. "Java home not set" is a very common error, especially for developers and users running Java applications from the command line.

Here’s a comprehensive breakdown of what it means, why it happens, and how to fix it on different operating systems.
What Does "Java Home Not Set" Mean?
In simple terms, it means that the operating system (or a specific program) doesn't know where the Java Development Kit (JDK) or Java Runtime Environment (JRE) is installed.
Java programs and build tools (like Maven, Gradle, or IDEs like IntelliJ/Eclipse) need to find two crucial things:
- The
javaexecutable: To run Java programs. - The
libdirectory: To find essential Java libraries (likert.jar).
The JAVA_HOME environment variable is the standard way to point to the root directory of your Java installation. For example, if Java is installed in C:\Program Files\Java\jdk-17.0.2, then JAVA_HOME should be set to that exact path.

Without this variable, tools can't automatically locate the necessary files, leading to the "Java home not set" or "JAVA_HOME is not defined correctly" error.
How to Fix It (Step-by-Step)
Follow the instructions for your operating system.
A. On Windows
Step 1: Verify Java is Installed First, make sure Java is actually installed and you know its location. Open Command Prompt or PowerShell and run:
java -version
If this command works and shows a version number, Java is installed. Now, find its location:

where java
This will output the full path to the java.exe file. For example: C:\Program Files\Java\jdk-17.0.2\bin\java.exe. The root directory is the part before \bin.
Step 2: Set the JAVA_HOME Environment Variable
- Press the Windows Key, type "environment variables", and select "Edit the system environment variables".
- In the System Properties window, click the "Environment Variables..." button.
- In the "System variables" section (lower half), click "New...".
- Variable name:
JAVA_HOME - Variable value: Paste the root directory path you found in Step 1 (e.g.,
C:\Program Files\Java\jdk-17.0.2). Do not include a trailing backslash! - Click OK on all windows to save.
Step 3: Update the PATH Variable
Many modern tools don't strictly require this, but it's good practice and ensures older tools work. It allows you to run java and javac from any directory.
- In the same "Environment Variables" window, find the
Pathvariable in the "System variables" list and click "Edit...". - Click "New" and add the path to Java's
bindirectory. For our example, this would be:C:\Program Files\Java\jdk-17.0.2\bin. - Important: Move the new Java
binentry to the top of the list to ensure it's found before any other Java installations. - Click OK on all windows.
Step 4: Verify the Fix Open a new Command Prompt or PowerShell window (the old one won't have the new variables) and run:
echo %JAVA_HOME%
It should print the path you set. Also, java -version should still work.
B. On macOS
Step 1: Verify Java is Installed Open the Terminal and run:
java -version
If it works, Java is installed. To find the location, use:
/usr/libexec/java_home -v
This command is very useful as it will print the active JDK's home path. For example, it might output: /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home.
Step 2: Set the JAVA_HOME Environment Variable
The best way to manage environment variables on macOS is with your shell profile file (.zshrc for macOS Catalina and later, or .bash_profile for older versions).
-
Open your profile file in a text editor. For Zsh:
open -e ~/.zshrc
For Bash:
open -e ~/.bash_profile
-
Add the following lines to the end of the file. Replace the path with the one from Step 1.
export JAVA_HOME=$(/usr/libexec/java_home -v 17) # Use the version you have, e.g., -v 11, -v 17 export PATH="$JAVA_HOME/bin:$PATH"
- Using
$(/usr/libexec/java_home -v 17)is dynamic and will automatically update if you switch JDK versions. - The
export PATHline makesjavaandjavacavailable system-wide.
- Using
-
Save the file and close the editor.
Step 3: Apply the Changes Either close and reopen your Terminal, or run the following command to load the changes into your current session:
source ~/.zshrc
Step 4: Verify the Fix In your Terminal, run:
echo $JAVA_HOME
It should print the correct path.
C. On Linux (Ubuntu/Debian)
Step 1: Verify Java is Installed Open a terminal and run:
java -version
If it works, Java is installed. To find the path, use the update-java-alternatives tool:
update-java-alternatives --list
This will list all installed Java versions and their paths. Look for the one you want to use, e.g., java-1.17.0-openjdk-amd64. The path will be something like /usr/lib/jvm/java-1.17.0-openjdk-amd64.
Step 2: Set the JAVA_HOME Environment Variable
-
Edit your shell profile file (e.g.,
~/.bashrc,~/.profile, or~/.pam_environmentfor a system-wide setting). We'll use~/.bashrcas an example.nano ~/.bashrc
-
Add the following lines to the end of the file. Replace the path with the one you found.
export JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64 # Use your actual path export PATH="$JAVA_HOME/bin:$PATH"
-
Save the file and exit the editor (in
nano, pressCtrl+X, thenY, thenEnter).
Step 3: Apply the Changes Close and reopen your terminal, or run:
source ~/.bashrc
Step 4: Verify the Fix In your terminal, run:
echo $JAVA_HOME
It should print the correct path.
Common Scenarios & Troubleshooting
-
IDE-Specific Error (e.g., IntelliJ, Eclipse): Sometimes the IDE fails to detect
JAVA_HOMEeven if it's set system-wide.- Fix: Open the IDE's configuration (e.g,
Help -> Edit Custom VM Optionsin IntelliJ) and explicitly add the-Djava.homeflag pointing to your JDK path. Or, in the IDE's project settings, manually set the JDK for the project.
- Fix: Open the IDE's configuration (e.g,
-
Build Tool Error (Maven, Gradle): The tool itself might be using a bundled JRE that doesn't have the necessary tools (like
javac).- Fix: Ensure your
JAVA_HOMEpoints to a full JDK, not just a JRE. A JDK includes the compiler (javac) and other development tools.
- Fix: Ensure your
-
Multiple Java Versions Installed: If you have multiple JDKs (e.g., JDK 8 and JDK 17), your
JAVA_HOMEmight be pointing to the wrong one.- Fix: Use tools like
sdkman(Linux/macOS) orjabba(cross-platform) to easily manage and switch between Java versions. On Windows, you can use the Windows Package Manager (winget) or manual configuration.
- Fix: Use tools like
-
Permissions Error: On Linux/macOS, you might get a "Permission denied" error when trying to run a script that relies on
JAVA_HOME.- Fix: Ensure the user has execute permissions on the directories in the
JAVA_HOMEpath.
- Fix: Ensure the user has execute permissions on the directories in the
