How to Check if Java Is Installed

The fastest check: open a terminal and run java -version. If it prints a version string, Java is installed and on your PATH. If it says "command not found" or "not recognized", Java may not be installed — or it may be installed but not on PATH.

The command

java -version

Installed and on PATH:

openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment Temurin-21.0.2+13

Not on PATH or not installed:

bash: java: command not found
# or on Windows:
'java' is not recognized as an internal or external command

Check the install directories

Windows:

Test-Path "C:\Program Files\Java"              # Oracle JDK
Test-Path "C:\Program Files\Eclipse Adoptium"  # Temurin
Test-Path "C:\Program Files\Microsoft"         # Microsoft OpenJDK

macOS:

ls /Library/Java/JavaVirtualMachines/

Linux:

ls /usr/lib/jvm/

Java installed but not on PATH

If the directory exists but java -version fails, the JDK is installed but the binary is not on PATH. Fix it:

Windows: Add %JAVA_HOME%\bin to the Path environment variable.

Mac:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

Linux:

sudo update-alternatives --config java

Check from a Java program

System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("java.version"));

Check whether javac (compiler) is also installed

javac -version

If java -version works but javac -version fails, you have a JRE (runtime only) and need to install the full JDK to compile Java code.