How to check Java version on Linux?

How to check Java version on Linux?

A terminal prints a Java version, but a background service still says Java is unavailable. I’m fascinated that the shell behind a command can change what Java an application sees.

The terminal’s result gives you a reference point to compare with the service’s launch context. Its meaning begins with what `java –version` tells you.

What java –version tells you

The java launcher can start a Java application. With the version option, it prints its product version and exits, identifying the runtime this shell resolves rather than every Java installation on disk.

The compiler is a separate tool, so its version is not a substitute for the runtime version.

CommandWhat it identifies
java –versionThe Java runtime launched by the resolved java command.
javac –versionThe compiler found by this shell.
readlink -f “$(command -v java)”The executable path behind the java command.

What you need before checking

Run this check in the same shell and user context as the application because a service or IDE may set its own environment. Otherwise, the terminal may resolve a different Java executable.

  • A terminal session with the same user and environment as the Java process you are checking.
  • A Java launcher available through that shell’s PATH. You do not need administrator privileges to print its version.

Run the version command your shell finds

Use the same shell that will launch the Java application, because its search path selects the executable.

1. Run java –version

Run the double-hyphen option to print the product version and exit without launching an application.

java --version
Output of java --version showing the active OpenJDK runtime
The java launcher reports the runtime this shell resolves.

The first line gives the product name and version, while the following lines identify runtime and virtual machine details. Those labels vary by distribution.

2. Use java -version when needed

If an older launcher rejects the double-hyphen option, try the single-hyphen form.

java -version

Oracle’s Java launcher reference lists both options. It documents java –version as writing to standard output and java -version as writing to standard error, which matters if a script redirects the output.

Why java and javac can report different versions

The java command starts an application, while javac compiles its source files. These are separate executables, so check the one your task actually uses.

Run a separate compiler check before compiling Java source.

javac --version

The Java Development Kit (JDK) includes development tools such as javac. A runtime-only installation may not include the compiler.

Oracle’s javac reference describes the compiler’s version option. I ran the runtime and compiler checks side by side, and they returned different version numbers, so treat each result as its own tool’s version.

When Java is missing or the wrong version appears

A failed lookup tells you what this shell can find, not everything installed on the machine. Match the symptom to the next check before changing a system setting.

What you seeWhat it meansNext check
Bash says it cannot find java.This shell did not resolve java through PATH. Java may be absent, or its executable directory may not be on the search path.Check the PATH environment variable and the Bash command-not-found error.
The runtime version differs from an application.The application may use another account, environment, or Java home setting.Compare the process environment with the JAVA_HOME setting.
You need a Java runtime but no launcher is available.There is no java command reachable in this shell.Follow the Linux OpenJDK installation guide for your distribution.

On Debian and Ubuntu, this command lists the Java targets registered with the alternatives system, as documented in the Debian alternatives manual. It does not list unregistered installations.

update-alternatives --list java

Trace the executable before changing anything

If the version is unexpected, find the exact executable behind the command before changing alternatives or reinstalling Java.

command -v java
readlink -f "$(command -v java)"

The first line shows the command selected by this shell, and the second follows its link to the executable. Once that path is clear, continue with the Linux command-line Java program steps in the same environment.

Frequently asked questions

Does java –version show the JDK version?

It reports the product version of the java launcher selected by this shell. Run javac –version to check the compiler separately.

How do I check the Java compiler version?

Run javac –version. It reports the compiler found by the current shell, which can differ from the runtime started by java.

Can Linux have more than one Java version installed?

Yes. On Debian and Ubuntu, update-alternatives –list java shows targets registered with that alternatives group, but it does not enumerate every manual installation.

Why does the terminal say java was not found?

The current shell could not resolve java through PATH. The executable may be missing or installed in a directory outside that shell’s search path.