Skip to content
Happy Programming Guide
Start learning
Java

Fixing “java is not recognized” after installing Java

Java installed but the terminal cannot find it? The fix is PATH and JAVA_HOME. Step by step for Windows, macOS and Linux, plus how to check which Java is running.

Lines of source code on a dark computer screen

You installed Java, typed java -version, and got 'java' is not recognized as an internal or external command — or on a Mac, command not found: java. The installer put Java on the disk but did not tell your terminal where to find it. Fixing that is a PATH entry and, for most tools, a JAVA_HOME variable.

What the error means#

When you type a command, the shell looks through a list of folders called the PATH. If none of them contains a file called java (or java.exe), you get this error. Java being installed somewhere on the disk is not enough; that folder has to be on the list.

Some installers add it for you. Many do not — particularly the plain JDK archives from Oracle, Adoptium and Microsoft.

Step 1: find where Java is#

POWERSHELL
# Windows - look in the usual places
dir "C:\Program Files\Java"
dir "C:\Program Files\Eclipse Adoptium"
dir "C:\Program Files\Microsoft"
Terminal
# macOS - the JDK tool lists every installed version
/usr/libexec/java_home -V

# Linux
ls /usr/lib/jvm
update-alternatives --list java

You are looking for a folder containing a bin subfolder with java inside it. That parent folder is what JAVA_HOME should point to; the bin folder is what goes on the PATH.

Windows#

Do this through the settings dialog so it survives reboots:

  1. Press the Windows key and type environment variables, then open Edit the system environment variables.
  2. Click Environment Variables.
  3. Under System variables, click New. Name: JAVA_HOME. Value: the JDK folder, for example C:\Program Files\Eclipse Adoptium\jdk-21.0.4.7-hotspot.
  4. Select Path, click Edit, then New, and add %JAVA_HOME%\bin.
  5. Click OK on every dialog.

Or from an administrator PowerShell:

POWERSHELL
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Eclipse Adoptium\jdk-21.0.4.7-hotspot", "Machine")
$p = [Environment]::GetEnvironmentVariable("Path", "Machine")
[Environment]::SetEnvironmentVariable("Path", $p + ";%JAVA_HOME%\bin", "Machine")

macOS#

Modern macOS uses zsh, so the file to edit is ~/.zshrc:

Terminal
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
java -version

The java_home helper finds the requested version wherever the installer put it, which is why it beats a hard-coded path. Drop the -v 21 to get whatever is newest.

If you installed with Homebrew and it still is not found, Homebrew tells you the exact line to add at the end of its install output — run brew info openjdk to see it again. Usually it is a symlink into /Library/Java/JavaVirtualMachines.

Linux#

If you installed from your distribution’s package manager, it normally handles this. If you unpacked a tarball:

Terminal
sudo mv jdk-21.0.4 /opt/
echo 'export JAVA_HOME=/opt/jdk-21.0.4' >> ~/.bashrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

On Debian and Ubuntu with several versions installed, update-alternatives is the cleaner way to choose the default:

Terminal
sudo update-alternatives --config java
sudo update-alternatives --config javac

Checking what you have#

Terminal
java -version        # the runtime that will execute programs
javac -version       # the compiler - only present with a JDK, not a JRE
echo $JAVA_HOME      # macOS/Linux
echo %JAVA_HOME%     # Windows cmd
$env:JAVA_HOME       # PowerShell
where java           # Windows: every java.exe on the PATH, first one wins
which -a java        # macOS/Linux equivalent

If java works but javac does not, you installed a runtime rather than a development kit. Install a JDK; it includes both.

When it works in one terminal but not another#

This is almost always because the variable was set for the wrong scope:

  • Windows: User variables apply to your account; System variables apply to everyone. Setting one and expecting it in an administrator prompt, or vice versa, is a common mismatch.
  • macOS: zsh reads ~/.zshrc; bash reads ~/.bash_profile. Editing the wrong one does nothing. Check with echo $SHELL.
  • VS Code and IntelliJ: launched from the Dock or Start menu, they may not inherit variables you set in a shell file. Log out and back in, or set the JDK inside the editor’s own settings as well.

Several versions installed#

Whichever bin folder comes first on the PATH wins. The where and which -a commands above show the order. If the wrong one is first, move your entry above it — on Windows, the PATH editor has Move Up buttons; on macOS and Linux, putting $JAVA_HOME/bin before $PATH in the export line does it.

If you switch versions often, a version manager is less painful than editing PATH by hand. SDKMAN on macOS and Linux and the Microsoft JDK installer on Windows both handle it.

Questions people ask#

Do I need JAVA_HOME if java already works?

Running programs does not need it. Maven, Gradle, Tomcat and most IDEs read it to find the JDK, so set it anyway; it costs nothing.

I set everything and it still says not recognized.

Open a brand-new terminal window. Then check for a typo in the path by pasting it into the file explorer. Then check the variable was set at the level you are using, User versus System.

Which Java version should I install?

The current long-term-support release. Java 25 is the latest LTS; Java 21 is still widely used and supported. Avoid the short-term releases in between unless you have a reason.

JDK or JRE?

JDK. It includes the compiler and the runtime. Standalone JREs are mostly a thing of the past.

Where to go next#

Java not working in VS Code, and the JDK setting that fixes itRead next

Keep reading

Java

Java Basics for Beginners

Java’s structure, types, control flow and the class-and-main-method boilerplate — explained so the ceremony makes sense rather than getting in the way.

3 min read

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *