How to Fix "JAVA_HOME is not set" Error on Windows, Mac, and Linux

Fixing the "JAVA_HOME is not set" Error: A Complete Guide

Encountering the error ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH is a common hurdle for developers. This error means your system can't find a Java Development Kit (JDK), which is essential for running tools like Gradle for Android development.

This guide will walk you through checking for a JDK, installing one if needed, and correctly setting the JAVA_HOME environment variable on Windows, macOS, and Linux.

1. Check if Java is Installed

First, verify if Java is already on your system but not properly configured.

  1. Open a Command Prompt (Windows) or Terminal (macOS/Linux).

  2. Run the following commands:

echo %JAVA_HOME%
java -version

If these commands return a version number, Java is installed. If you get an error like command not found, you need to install a JDK.

2. Install a JDK (If Needed)

If you don't have a JDK, you need to install one.

  • For Android Development: The easiest method is to use the JDK bundled with Android Studio. You can find its path in File > Project Structure > SDK Location > Gradle Settings > Gradle JDK.

  • General Use: Download and install a standalone JDK like OpenJDK 11 or 17 from adoptium.net or another trusted vendor.

3. How to Set JAVA_HOME

The JAVA_HOME variable must point to the root directory of your JDK installation (e.g., the folder containing the bin directory). Here’s how to set it on your OS.

Windows:

  1. Find Your JDK Path: Common paths include:

    • Android Studio JDK: C:\Program Files\Android\Android Studio\jbr

    • Manual JDK Install: C:\Program Files\Java\jdk-11.0.15

  2. Set the JAVA_HOME Variable:

    • Search for "Edit environment variables" in the Start Menu.

    • Click "Environment Variables...".

    • Under "System variables", click "New...".

    • Variable name: JAVA_HOME

    • Variable value: Your JDK path (e.g., C:\Program Files\Android\Android Studio\jbr)

    • Click OK.

  3. Update the PATH Variable:

    • In the same window, find and select the Path variable, then click "Edit...".

    • Click "New" and add this entry: %JAVA_HOME%\bin

    • Click OK on all dialogs to save.

macOS:

Find JDK Path:

  • Android Studio's bundled JDK is often within the app package: /Applications/Android Studio.app/Contents/jbr/Contents/Home.

  • If you installed a JDK using Homebrew, it might be in /usr/local/opt/openjdk@11 or /Library/Java/JavaVirtualMachines/.

  • To find all installed JDKs, you can run: /usr/libexec/java_home -V

Set JAVA_HOME:

  1. Open your shell configuration file. This is usually ~/.zshrc (for Zsh, the default in newer macOS) or ~/.bash_profile or ~/.bashrc (for Bash).

  2. Add the following line, replacing the path with your actual JDK path:

export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
# Or for a manually installed JDK:
# export JAVA_HOME=$(/usr/libexec/java_home -v 11) # For JDK 11, for example
Update PATH Variable:
Add JAVA_HOME/bin to your PATH in the same file:
export PATH="$JAVA_HOME/bin:$PATH"

Apply Changes:

  1. Save the file.

  2. Source the file to apply changes to your current terminal session: source ~/.zshrc (or source ~/.bash_profile)

Verify:
Open a new terminal window and type:

echo $JAVA_HOME
java -version

Linux:

Find JDK Path:

  • Android Studio's bundled JDK might be in /opt/android-studio/jbr or similar, depending on your installation method.

  • If installed via a package manager, it could be in /usr/lib/jvm/java-11-openjdk-amd64/ or similar.

  • Use update-alternatives --config java to see available Java installations.

Set JAVA_HOME:

  1. Edit your shell configuration file (e.g., ~/.bashrc, ~/.zshrc, or /etc/environment for system-wide).

  2. Add the following line, replacing the path:

export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" # Example path

Update PATH Variable:
Add JAVA_HOME/bin to your PATH:

export PATH="$JAVA_HOME/bin:$PATH"

Apply Changes:

  1. Save the file.

  2. Source the file: source ~/.bashrc (or your respective shell config file).

Verify:
Open a new terminal window and type:

echo $JAVA_HOME
java -version

 

Important Notes

  • Restart Everything: After setting the variables, restart your terminal, IDE (like Android Studio), and any other development tools to ensure they pick up the new environment variables.

  • Path Matters: JAVA_HOME must point to the JDK root folder, not the bin folder inside it. The bin folder should be in your PATH variable.

← Back to Home

Comments

Post a Comment