Of course! "JAVA_HOME is not set" is a very common error, especially when working with Java-based tools like Maven, Gradle, Tomcat, or when trying to compile Java code from the command line.
Let's break down what this means, why it's important, and how to fix it on different operating systems.
What is JAVA_HOME?
Think of JAVA_HOME as a pointer or an environment variable that tells your operating system and other programs where the Java Development Kit (JDK) is installed.
- It's a Path:
JAVA_HOMEstores the root directory of your JDK installation (e.g.,C:\Program Files\Java\jdk-17.0.2on Windows or/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Homeon macOS). - Why is it needed? Many Java applications don't want to guess where Java is. Instead, they look for the
JAVA_HOMEvariable to find the JDK'sbindirectory, which contains essential tools likejava,javac,jar, etc. This makes your setup portable and reliable.
How to Fix It: Step-by-Step Guide
The solution involves three main steps:
- Install the JDK (if you haven't already).
- Find the Installation Path of the JDK.
- Set the
JAVA_HOMEEnvironment Variable.
Step 1: Install the JDK
If you don't have a JDK installed, you need to get one first. The most common source is Oracle's JDK, but you can also use OpenJDK.

- Go to the Oracle JDK Download Page or the Eclipse Temurin (OpenJDK) Download Page.
- Download the latest version (or the version required by your project) for your operating system.
- Run the installer. Remember the installation path, or use the default one.
Step 2: Find Your JDK Installation Path
You need to know exactly where the JDK was installed.
On Windows:
- Open File Explorer.
- Navigate to
C:\Program Files\Java. You should see a folder named something likejdk-17,jdk-11, etc. - Click into that folder. The full path is your
JAVA_HOMEpath. It will look something like this:C:\Program Files\Java\jdk-17.0.2
On macOS:
- Open Finder.
- From the top menu, go to Go -> Go to Folder... (or press
Cmd + Shift + G). - Type
/Library/Java/JavaVirtualMachinesand click Go. - You will see a folder like
jdk-17.jdk. Click into it, then intoContents, and thenHome. - The full path is your
JAVA_HOMEpath. It will look something like this:/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
On Linux (Ubuntu/Debian):
The JDK is often installed in a directory under /usr/lib/jvm/ or /usr/lib/jvm/java- followed by the version.
- Open a terminal.
- List the contents of the directory:
ls /usr/lib/jvm/
- You'll see something like
java-11-openjdk-amd64orjdk-17.0.2+7. The path is/usr/lib/jvm/followed by that folder name. For example:/usr/lib/jvm/java-11-openjdk-amd64(Note: On some systems, the
JAVA_HOMEpath might just be/usr/lib/jvm/java-11-openjdk-amd64or/usr/lib/jvm/jdk-17.0.2+7)
Step 3: Set the JAVA_HOME Environment Variable
This is the core step. The process is different for each OS.

On Windows:
For Windows 10 / 11:
- Press the Windows key, type "environment variables", and select "Edit the system environment variables".
- In the System Properties window, click the "Environment Variables..." button.
- Under the "System variables" section (top half), click "New...".
- In the "New System Variable" window:
- Variable name:
JAVA_HOME - Variable value: Paste the JDK path you found in Step 2 (e.g.,
C:\Program Files\Java\jdk-17.0.2).
- Variable name:
- Click OK on all windows.
For Windows 7 / 8: The process is very similar. Use the Start Menu search to find "Edit the system environment variables".
On macOS:
The best way to manage environment variables on macOS is using your shell's configuration file (.zshrc for modern macOS, .bash_profile or .bashrc for older versions).
- Open Terminal.
- Determine your shell by typing:
echo $SHELL. If it says/bin/zsh, you'll use.zshrc. If it says/bin/bash, you'll use.bash_profile. - Open the correct configuration file in a text editor (like Nano or VS Code). This example uses
.zshrc:nano ~/.zshrc
- Add the following lines to the end of the file. Replace the path with your own!
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home export PATH="$JAVA_HOME/bin:$PATH"
- The
export PATHline is important. It adds the JDK'sbindirectory to your system's PATH, so you can runjavaandjavacfrom anywhere.
- The
- Save the file and exit the editor (in Nano, press
Ctrl + X, thenY, thenEnter). - Apply the changes by restarting your terminal or by running:
source ~/.zshrc
On Linux (Ubuntu/Debian):
- Open a terminal.
- You'll need
sudoto edit system-wide files. Open the/etc/environmentfile with a text editor like Nano:sudo nano /etc/environment
- Add the following line to the file. Replace the path with your own!
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Note: Do not use
exportin this file. The syntax is different.
- Save the file and exit (
Ctrl + X, thenY, thenEnterin Nano). - Log out and log back in for the changes to take effect, or reboot your system. Alternatively, you can run
source /etc/environment, but logging out is more reliable.
Step 4: Verify the Fix
After setting the variable, you must verify it was set correctly.
-
Open a new terminal/command prompt to ensure you are in a clean environment.
-
Check the value of
JAVA_HOME:- Windows: In Command Prompt or PowerShell, type:
echo %JAVA_HOME%
- macOS / Linux: In Terminal, type:
echo $JAVA_HOME
This should print the path you set in Step 2.
- Windows: In Command Prompt or PowerShell, type:
-
Check if Java is found:
- Type the following commands. They should work without any "command not found" errors.
java -version javac -version
If these commands run and show the correct Java version, you have successfully set
JAVA_HOME
- Type the following commands. They should work without any "command not found" errors.
