Java version checks matter when a JAR refuses to start, a build tool selects the wrong compiler, or a server has several JVMs installed. To check Java version in Linux, start with java -version, then confirm the compiler, active path, and JAVA_HOME only when the problem points there.
Check Java Version in Linux
Use java -version to print the Java runtime that the current shell will run. Java writes this version banner to standard error, so the output can still appear even when you redirect normal output elsewhere.
java -version
Example output:
openjdk version "25.0.3" 2026-04-21 OpenJDK Runtime Environment (Red_Hat-25.0.3.0.9-1) (build 25.0.3+9) OpenJDK 64-Bit Server VM (Red_Hat-25.0.3.0.9-1) (build 25.0.3+9, mixed mode, sharing)
The first quoted value is the Java release branch and patch level. In this output, 25.0.3 means Java 25 with the third update release for that branch. Older Java 8 installations use the legacy 1.8.0 format, which still means Java 8.
Modern Java releases also accept the long option:
java --version
Keep java -version as the safest default when a command may run on older Java 8 systems, because the long option was added later.
Read the Java Version Output
| Output Part | Meaning | Reader Decision |
|---|---|---|
openjdk version "25.0.3" | Installed Java runtime branch and update level. | Compare this value with the version your application requires. |
Runtime Environment | The runtime package that starts Java applications. | Confirms java exists, but not that a compiler is installed. |
Server VM | The JVM implementation and architecture-specific virtual machine. | Useful when troubleshooting vendor or 64-bit runtime requirements. |
1.8.0_... | Legacy Java 8 version style. | Treat it as Java 8, not Java 1. |
Check the Java Compiler Version
java runs Java applications. javac compiles Java source code. Build tools such as Maven and Gradle often need the compiler, so check javac separately when a project fails during compilation.
javac -version
If java -version works but javac -version returns command not found, the system probably has a JRE or headless runtime package instead of a full JDK. Install a JDK package for your distribution, then retest both commands.
Use Common Java Version Commands
Different Java problems need different checks. Use the smallest command that answers the immediate question before changing packages, alternatives, or shell profiles.
| Task | Command | What It Confirms |
|---|---|---|
| Show the active runtime | java -version | Runtime branch used by the current shell. |
| Use the newer long option | java --version | Runtime branch on Java 9 and newer systems. |
| Show the active compiler | javac -version | JDK compiler branch, when a JDK is installed. |
| Find the Java command path | command -v java | First java executable found on $PATH. |
| List every shell match | type -a java | All matching commands, functions, aliases, or PATH entries. |
| Resolve the real target | readlink -f "$(command -v java)" | Actual JVM binary behind the symlink. |
| Print Java home from properties | java -XshowSettings:properties -version 2>&1 | grep 'java.home' | Runtime’s own java.home property. |
Use command -v for scripts because it is shell-aware and avoids the extra assumptions of which. When you need a broader path search or a comparison with related lookup tools, the which command guide covers those lookup differences in more detail.
Find Which Java Command Linux Runs
Multiple Java installs can coexist through package managers, SDKMAN, asdf, vendor tarballs, IDE bundles, or manually copied binaries. The shell runs the first matching command on $PATH, so check the path before assuming the wrong Java package is installed.
command -v java
List every matching Java command when PATH order may be the issue:
type -a java
Resolve the symlink to the actual JVM binary:
readlink -f "$(command -v java)"
Example output:
/usr/lib/jvm/java-25-openjdk/bin/java
If the resolved path starts under $HOME/.sdkman, $HOME/.asdf, /opt, or another user-managed prefix, your package manager’s alternatives setting may not control that Java command. Adjust PATH order or use the matching tool’s own version-selection command instead of forcing a system-wide alternatives change.
Check JAVA_HOME for Java Applications
JAVA_HOME is not required for normal java execution, but build tools, application servers, IDE integrations, and shell scripts often look for it. Check whether the current shell has the variable set before editing profile files.
printenv JAVA_HOME
Derive a temporary JAVA_HOME value from the active java command when the application only needs the current terminal session:
export JAVA_HOME="$(dirname "$(dirname "$(readlink -f "$(command -v java)")")")"
printf '%s\n' "$JAVA_HOME"
"$JAVA_HOME/bin/java" -version
Use a persistent shell-profile entry only when a workflow truly needs it. Ubuntu users can follow the Ubuntu Java environment path setup, while Fedora users can use the Fedora Java environment path setup for distro-specific profile and alternatives behavior.
Install Missing Java Commands
A missing java command means either Java is not installed or the installed binary is not on $PATH. A missing javac command usually means the runtime is installed without the development package.
Package names differ by distribution. On Ubuntu, Fedora, and Rocky Linux, install the default or versioned OpenJDK development package from the distro repositories. On another distribution, install the JDK or OpenJDK development package that matches the Java branch required by your application, then retest both version commands.
Install a JDK on Ubuntu
Ubuntu’s default-jdk package installs the default OpenJDK branch for the release and includes both java and javac.
sudo apt update
sudo apt install default-jdk
Use a branch-specific OpenJDK package when an application requires a fixed release, such as OpenJDK 21 on Ubuntu or OpenJDK 25 on Ubuntu.
Install a JDK on Fedora
Fedora uses versioned OpenJDK package names for pinned branches. Install the development package when javac, jar, or build tools need a compiler.
sudo dnf install java-25-openjdk-devel
Use the OpenJDK on Fedora guide before choosing a moving package such as java-latest-openjdk-devel, because that package can track a newer feature release than the stable branch your application expects.
Install a JDK on Rocky Linux 10
Rocky Linux 10 exposes versioned OpenJDK packages in AppStream. Pick the branch your application or build pipeline requires, then verify both runtime and compiler versions.
sudo dnf install java-21-openjdk-devel
Use Java 25 instead when the application stack already targets that branch:
sudo dnf install java-25-openjdk-devel
Retest After Installing Java
Confirm the runtime and compiler from the same shell after installation:
java -version
javac -version
If the shell still reports java: command not found immediately after installation, clear Bash’s command lookup cache and check the path again:
hash -r
command -v java
Manage Multiple Java Versions
Alternatives systems manage package-owned Java symlinks, but they do not control every user-managed Java install. Check the active path first, then switch both the runtime and compiler when package-owned alternatives are the right tool.
type -a java
readlink -f "$(command -v java)"
On Ubuntu, select the default runtime and compiler with update-alternatives:
sudo update-alternatives --config java
sudo update-alternatives --config javac
On Fedora and Rocky Linux, use the RHEL-family alternatives command:
sudo alternatives --config java
sudo alternatives --config javac
Retest both commands after switching. Selecting only java can leave javac on another branch, which is enough to break source compatibility checks during builds.
java -version
javac -version
Check Java Used by Build Tools and Services
The terminal’s Java version is not always the same version a build tool, service account, or application wrapper uses. Check the tool or user context that actually runs the Java workload.
Check Maven or Gradle Java Versions
Maven and Gradle print their Java runtime in their version output, which helps when a build fails even though java -version looks correct in your shell.
mvn -version
gradle -version
If Maven itself is missing, use the matching distro workflow for Apache Maven on Ubuntu, Apache Maven on Fedora, or Apache Maven on Rocky Linux.
Check Java for a Service User
A systemd service, CI runner, or application server can run as another user and see a different PATH. Replace appuser with the service account that starts the Java workload.
sudo -u appuser java -version
sudo -u appuser sh -lc 'command -v java'
Check the service environment when a unit file sets JAVA_HOME or overrides PATH:
systemctl cat service-name.service
systemctl show service-name.service -p Environment
Check a Specific Java Binary
Run a known Java binary directly when PATH order is not trustworthy or an application must use a pinned JDK path:
"$JAVA_HOME/bin/java" -version
Troubleshoot Java Version Problems
Fix java Command Not Found
The error java: command not found means the shell cannot find a runnable java command. Start with a path check before installing another package.
command -v java
No output means the command is unavailable in the current shell. Install a JDK from your distribution’s package manager or adjust PATH if Java is installed under a user-managed prefix such as SDKMAN, asdf, or a vendor directory.
java -version
Fix javac Command Not Found
The error javac: command not found usually means the runtime package is installed without the JDK compiler. Confirm the compiler is absent, then install a development JDK package for the Java branch your project needs.
command -v javac || printf '%s\n' 'javac is missing'
Retest the compiler after installing the JDK:
javac -version
Fix Java Version Older Than Expected
An older Java version usually comes from PATH order, alternatives selection, or a user-managed toolchain taking priority over the package-managed binary. Identify the active command before changing packages.
type -a java
readlink -f "$(command -v java)"
java -version
If the resolved path is package-owned, switch with update-alternatives or alternatives. If the path is under your home directory, use the version manager that created it or move that PATH entry behind the package-managed Java path.
Fix JAVA_HOME Pointing to the Wrong Java
A stale JAVA_HOME can make Maven, Gradle, Tomcat, or an IDE use a different Java branch than your terminal. Check whether the variable points to an executable Java binary.
printenv JAVA_HOME
if [ -n "$JAVA_HOME" ]; then
test -x "$JAVA_HOME/bin/java" && "$JAVA_HOME/bin/java" -version
else
printf '%s\n' "JAVA_HOME is not set"
fi
Replace a stale profile entry with the path returned by the active Java binary, or unset JAVA_HOME for tools that should follow the current java command automatically.
Fix UnsupportedClassVersionError
java.lang.UnsupportedClassVersionError means the application was compiled for a newer Java release than the runtime can execute. Check the runtime first, then switch or install the Java branch required by the application.
java -version
If a JDK is installed and you have the failing class file, javap can show the class file’s major version. A higher major version than your runtime supports confirms that the application needs a newer Java release.
javap -verbose MyClass.class | grep 'major version'
Conclusion
When the runtime, compiler, executable path, and Java home value line up, Java applications and build tools use the branch you expect. If they do not, fix the smallest mismatch first: install the missing JDK package, adjust PATH, correct JAVA_HOME, or switch package-owned alternatives for both java and javac.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>