Skip to content

java: command not found After Installing the JDK, Fixed

java: command not found means your shell searched every PATH directory and none had java in it. Why setting JAVA_HOME doesn't help, how to see what the shell sees, and the fix for Linux, macOS, sudo, cron and Docker.

java-command-not-found path java-home bash jdk-setup java-errors linux macos
Gopi Gorantala
java: command not found After Installing the JDK, Fixed
Reading Progress

On This Page

The line your shell printed

$ java -version
bash: java: command not found
$ echo $?
127

Your JDK is fine. Your shell can't see it.

That's the whole meaning of java: command not found. Bash went through every directory in PATH, found no executable called java in any of them, and gave up with exit code 127. Java never ran. Nothing in that message came from the JVM, the JDK installer, or Oracle. It came from the shell.

It looks slightly different depending on what printed it:

bash: java: command not found          # bash, interactive (Linux, older macOS)
bash: line 1: java: command not found  # bash -c or a script
dash: 1: java: not found               # /bin/sh on Debian/Ubuntu, Docker RUN lines
zsh: command not found: java           # zsh, the macOS default shell

The first three are from my lab (bash 5.2.21 and dash on Ubuntu 24.04). The zsh line is zsh's standard wording; I didn't run zsh for this. On a Mac with no JDK at all you usually don't get any of these, because /usr/bin/java exists as a stub and prints The operation couldn't be completed. Unable to locate a Java Runtime. instead. Different problem, covered further down.

You almost always meet this on a laptop, minutes after you installed something. Sometimes on a fresh CI runner or Docker image. Rarely anywhere else, because by production somebody has already fixed it once.

Thirty seconds to find out what the shell sees

My first instinct the first time, years ago, was that the installer had failed. It hadn't. The JDK was sitting in /opt, perfectly healthy. I'd unpacked a tarball and assumed that was "installing".

Before you reinstall anything, ask the shell three questions:

command -v java || echo "not on PATH"     # what would run, if anything
echo "$PATH" | tr ':' '\n'                # every directory it searches, in order
ls "$JAVA_HOME/bin/java" 2>/dev/null || echo "JAVA_HOME doesn't point at a JDK"

If command -v prints nothing and the ls finds the file, you've got your answer: the JDK exists, and its bin directory isn't in PATH. That's nine out of ten cases. If JAVA_HOME is empty too, find the JDK first (/usr/lib/jvm, /opt, ~/.sdkman/candidates/java, or /usr/libexec/java_home -V on a Mac).

Reproduce it with a clean PATH

You can produce every variant of this on any Linux box without uninstalling anything. The trick is env -i, which starts a shell with an empty environment, so you control PATH exactly. I used Ubuntu 24.04, bash 5.2.21 and OpenJDK 21.0.13; any JDK 17+ behaves the same, because none of this touches Java.

Step 1: a fake "installed" JDK and a PATH without it.

mkdir -p ~/pathlab/bin ~/pathlab/opt
for t in bash dash ls cat head mv mkdir; do ln -s "$(command -v $t)" ~/pathlab/bin/$t; done
cp -a /usr/lib/jvm/java-21-openjdk-amd64 ~/pathlab/opt/jdk-21   # what unpacking a tarball gives you
L=~/pathlab

I ran it under a scratch directory; the paths in the output below are rewritten to ~/pathlab so they read cleanly. Everything else is verbatim.

Step 2: run java the way a new user does.

$ env -i PATH=$L/bin bash --norc -c 'java -version; echo "exit=$?"'
bash: line 1: java: command not found
exit=127

Step 3: set JAVA_HOME, the way half the tutorials tell you to.

$ env -i PATH=$L/bin JAVA_HOME=$L/opt/jdk-21 bash --norc -c 'echo $JAVA_HOME; java -version; echo "exit=$?"'
/home/you/pathlab/opt/jdk-21
bash: line 1: java: command not found
exit=127

That's the lab's main point. JAVA_HOME is set, correct, and ignored.

Step 4: put the JDK's bin directory on PATH.

$ env -i PATH=$L/bin JAVA_HOME=$L/opt/jdk-21 bash --norc -c 'export PATH="$JAVA_HOME/bin:$PATH"; java -version; command -v java'
openjdk version "21.0.13.1" 2026-11-17
OpenJDK Runtime Environment (build 21.0.13.1+2-1-24.04.1-Ubuntu)
OpenJDK 64-Bit Server VM (build 21.0.13.1+2-1-24.04.1-Ubuntu, mixed mode, sharing)
/home/you/pathlab/opt/jdk-21/bin/java

Step 5: the export that doesn't stick. This is the dead end I hit on that first laptop. I put the export in a setenv.sh, ran ./setenv.sh, and java still wasn't found. It looked like it should work. Here's why it can't:

$ env -i PATH=$L/bin bash --norc -c 'bash -c "export PATH=$L/opt/jdk-21/bin:\$PATH; java -version 2>&1 | head -1"; java -version'
openjdk version "21.0.13.1" 2026-11-17
bash: line 1: java: command not found

The child shell found java. The parent never saw the change. A script you run with ./ is a child; source setenv.sh runs it in your current shell.

The confirmation signal is always the same pair: the message above and exit code 127. If you get 126 instead (Permission denied), the file was found but isn't executable, which is a different fix (chmod +x, or a noexec mount).

Teardown is rm -rf ~/pathlab. Nothing outside it changed.

The short answer

The shell finds commands by searching the directories in PATH. Installing or unpacking a JDK puts java in some directory, and unless that directory is in PATH (or a package manager symlinked java into one that is), the shell won't find it. JAVA_HOME is a separate variable that Maven, Gradle and other tools read. The shell doesn't.

How the shell looks for java, and why JAVA_HOME isn't part of it

When you type java, bash checks, in order: aliases and functions, builtins, its hash table of commands it found before, and then each directory in PATH, left to right. The first executable file named java wins. If none matches, it prints %s: command not found (the format string lives in bash's execute_cmd.c) and sets status 127. POSIX fixes those numbers: 127 for "not found", 126 for "found but couldn't execute". You'll see the same 127 from sh, dash, zsh and from Docker.

So where does JAVA_HOME come in? Nowhere in that list. It's a convention, not a shell feature. The mvn launcher script reads it to decide which java to run. Gradle's wrapper reads it. Tomcat's catalina.sh reads it. Your shell has never heard of it. That's why the most common fix online, "set JAVA_HOME", leaves you exactly where you were until you also write export PATH="$JAVA_HOME/bin:$PATH". And while you're there: JAVA_HOME is the JDK's root directory (/usr/lib/jvm/java-21-openjdk-amd64), never .../bin and never .../bin/java. I've seen that last one in a popular tutorial. It breaks every tool that appends /bin/java itself.

Two variants surprised me in the lab.

The hash table. Bash remembers where it found java. If you uninstall that JDK without touching PATH, bash keeps trying the old path:

bash: line 4: /home/you/pathlab/opt/old-jdk/bin/java: No such file or directory
exit=127

That's not "command not found", but it's the same class of mistake and the same exit code. hash -r clears it. Assigning PATH clears it too, which is why export PATH=... sometimes seems to fix things on its own.

The second one is nastier. I patched a copy of the java launcher so its ELF header named the Alpine (musl) loader, /lib/ld-musl-x86_64.so.1, which doesn't exist on Ubuntu. That's what happens when you copy a glibc-built JDK into an Alpine image, or the reverse. The file is there, it's executable, it's first on PATH:

$ java -version          # bash 5.2
bash: line 1: /home/you/pathlab/opt/fake-musl-jdk/bin/java: cannot execute: required file not found
$ java -version          # dash, same PATH
dash: 1: java: not found

Dash says not found for a file you can ls. BusyBox sh on Alpine does the same (I didn't run Alpine here; Docker Hub wasn't reachable from my sandbox). If you're staring at sh: java: not found inside a container and ls -l shows the file, run file $(command -v java) and read the interpreter field. The fix is a JDK built for that libc, not anything to do with PATH.

Fixing java: command not found on Linux, macOS and in containers

Confirm the JDK exists first, with the commands from the triage section. Then pick your case.

Linux

If you installed from your distro (sudo apt install openjdk-21-jdk, sudo dnf install java-21-openjdk-devel), the package registers java with update-alternatives and links it into /usr/bin. If java is missing after that, you probably installed only a -headless JRE package without javac, or the link points at something removed. Check with:

update-alternatives --display java
sudo update-alternatives --config java    # choose among installed JDKs

If you unpacked a tarball (Temurin, Oracle, a zip from a vendor), nothing registered it. Add it yourself, for your user:

# ~/.bashrc (interactive shells); also source it from ~/.profile if you log in on a console
export JAVA_HOME=/opt/jdk-21.0.5
export PATH="$JAVA_HOME/bin:$PATH"

Then open a new terminal, or source ~/.bashrc. For every user on the box, a file in /etc/profile.d/jdk.sh with the same two lines works on most distros.

macOS

With the Oracle or Temurin .pkg installers, the JDK lands in /Library/Java/JavaVirtualMachines, and the /usr/bin/java stub finds it on its own. If you see Unable to locate a Java Runtime, the stub found no JDK there. That happens most often with Homebrew, whose openjdk formula isn't linked into that directory. Brew's own post-install note tells you to add the symlink:

sudo ln -sfn "$(brew --prefix)/opt/openjdk/libexec/openjdk.jdk" /Library/Java/JavaVirtualMachines/openjdk.jdk
/usr/libexec/java_home -V          # should now list it

If you get zsh: command not found: java instead, /usr/bin itself isn't in your PATH, or you replaced PATH wholesale in a dotfile. Put this in ~/.zprofile (Terminal and iTerm open login shells) or ~/.zshrc:

export JAVA_HOME="$(/usr/libexec/java_home -v 21)"
export PATH="$JAVA_HOME/bin:$PATH"

I didn't run these on a Mac for this post; the commands are from Apple's java_home man page and Homebrew's caveat text.

sudo, cron, systemd and CI

It works in your terminal and fails under sudo. That's secure_path. On Ubuntu 24.04, /etc/sudoers has Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin", so a JDK that's only on your personal PATH vanishes. Use sudo "$JAVA_HOME/bin/java" or sudo env "PATH=$PATH" java.

Cron, systemd and CI steps don't read your ~/.bashrc at all. Give them an absolute path (ExecStart=/opt/jdk-21.0.5/bin/java -jar app.jar) or set Environment=PATH=... in the unit. In a Dockerfile, ENV is the only thing that counts:

ENV JAVA_HOME=/opt/java/openjdk
ENV PATH="${JAVA_HOME}/bin:${PATH}"

The official eclipse-temurin images already do this. Custom images that copy a JDK in by hand usually forget the second line.

What each fix costs: almost nothing, apart from one thing. Prepending $JAVA_HOME/bin means that JDK beats whatever /usr/bin/java points to, which is usually what you want, until you install a second JDK and forget which one wins. That's the next problem people hit, and the reason I keep one place per machine that decides it.

Set it once, in the right place

The pattern I've settled on: exactly one line in one file decides JAVA_HOME, and PATH is always derived from it. Never hardcode /opt/jdk-21.0.5/bin into PATH separately, because the two will drift on the next upgrade. If you switch JDKs often, let a tool own both: SDKMAN on Linux and macOS (sdk use java 21.0.5-tem), or jenv. Both rewrite JAVA_HOME and PATH together.

For anything that isn't a human at a terminal, skip the shell lookup entirely. A systemd unit, a cron line, a container ENTRYPOINT should name java by absolute path or set PATH explicitly. Dotfiles are for people.

The check that would have caught it in CI

Make the first step of every build print what it's actually using:

set -eu
command -v java javac
java -version
echo "JAVA_HOME=${JAVA_HOME:-unset}"

command -v javac matters more than it looks. A runner image with only a JRE passes java -version and then fails later with Maven's No compiler is provided in this environment. Catching it on line two saves a confusing log.

Version-wise, nothing here depends on the JDK. I checked bash 5.2.21 and dash on Ubuntu 24.04 with OpenJDK 21.0.13, and the shell messages don't change between JDK 8 and 25. What does change across versions is bash's wording for the missing-loader case and which shell your OS defaults to (zsh on macOS since Catalina). If you upgrade the JDK by unpacking a new tarball, JAVA_HOME is the one line to update.

If you remember nothing else

  • command not found comes from the shell. Java didn't run.
  • JAVA_HOME is for build tools. The shell only reads PATH, so add $JAVA_HOME/bin to it.
  • An export inside ./script.sh dies with the script. source it, or put it in your shell's startup file and open a new terminal.
  • sudo, cron, systemd and Docker don't read your dotfiles. Use absolute paths there.
  • sh: java: not found for a file that exists means the wrong libc build, usually glibc JDK on Alpine.

Questions people search for

How do I fix java: command not found on Ubuntu? Install a JDK package (sudo apt install openjdk-21-jdk) so java is linked into /usr/bin, or, for a tarball, add export PATH="$JAVA_HOME/bin:$PATH" to ~/.bashrc and open a new terminal. Check the result with command -v java.

I set JAVA_HOME, why does it still say java: command not found? Because the shell never reads JAVA_HOME. It only searches PATH. Add the JDK's bin directory to PATH, and make sure JAVA_HOME is the JDK root, not .../bin/java.

Why does java work in my terminal but not with sudo or in cron? sudo replaces your PATH with secure_path, and cron and systemd start with a minimal environment that ignores your dotfiles. Call java by absolute path, or set PATH in the unit or crontab.

What does exit code 127 mean for java? The shell couldn't find the command, or (in the missing-loader case) couldn't load it. 126 means it was found but couldn't be executed, usually a missing execute bit. Neither one involves the JVM. If the JVM itself fails to start, you get its own messages, like UnsupportedClassVersionError or Could not find or load main class.

Calling java from code fails the same way: ProcessBuilder on JDK 21 throws java.io.IOException: Cannot run program "java": Exec failed, error: 2 (No such file or directory) when the child's PATH lacks it. I covered how child processes inherit their environment in running commands with ProcessBuilder and reading environment variables in Java. If you're unsure whether you installed a JDK or a JRE, JDK vs JRE vs JVM sorts that out.

java-command-not-foundpathjava-homebashjdk-setupjava-errorslinuxmacos

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments