How to fix “java command not found”

Java Runtime Environment (JRE) is a powerful set of software that allow you to develop Java applications quickly and easily. It includes the Java Virtual Machine (JVM), essential core classes and libraries, and everything else you need to get started with Java. JRE is a part of the Java Development Kit (JDK), but can be installed separately. The Java Runtime Environment was originally created by Sun Microsystems Inc., a subsidiary of Oracle Corporation. It’s also known as the Java Runtime.

Installing Java in Linux should be easy using package managers, such as apt, yum, or pacman, depending on your Linux distribution. However, there are times that you may encounter "java: command not found" error, even though you think you have installed it. The full error message may look something like this :

Command 'java' not found, but can be installed with:

1.  sudo apt install default-jre
2.  sudo apt install openjdk-11-jre-headless
3.  sudo apt install openjdk-8-jre-headless

In this article, we will show you a few solutions that you can use to fix java command not found error.

Add Java to PATH environment variable

Most operating systems look for executables and binaries in a specific set of places. The list of all those places is stored in an environment variable, often named PATH.

If you knew where the Java binary is placed, you can add one of the Java path to PATH variable using the following command.

export PATH=$PATH:/path/to/java/binary/

Command 'java' not found, but can be installed with

Reinstall JRE

The quickest way to fix java command not found error is to reinstall JRE. In order to do so, run the following command:

sudo apt install default-jre

There are other alternative packages that you can choose instead of default-jre, which installs the latest version of JRE (11 at the time of this writing).

You can choose to install one of the two LTS version of JRE (8 and 11) by installing openjdk-8-jre and openjdk-11-jre. There are also headless packages that don’t provide any graphical functionalities, which often used in servers.

The difference between Java packages can quickly be summed up as the following:

  • The openjdk-<version>-jre-headless package is used when you don’t need to draw anything in the graphical user interface. For example a background service or a terminal application. It’s commonly used on servers.
  • The openjdk-<version>-jre package includes the openjdk-<version>-jre-headless package since it contains most of the Java features. Desktop users should install this package.
  • The openjdk-<version>-jre package provides more functionality, but heavier than its headless counterpart. It’s not recommended to install on a server since it installs extra software that is not commonly used on a server.

Replacing the version number allows you to install multiple versions of Java in the same machine. In that case, you can run sudo update-alternatives --config java to manage which version you want to use at the exact moment.

There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                      Priority   Status
* 0            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      auto mode
  1            /usr/bin/gij-4.4                           1044      manual mode
  2            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
  3            /usr/lib/jvm/java-6-sun/jre/bin/java       63        manual mode

Press enter to keep the current choice[*], or type selection number:

We hope that the information above is useful to you and helped you fix java command not found error. If you have any questions, then please feel free to ask in the comments below.

Click to rate this post!
[Total: 13 Average: 4.8]

Leave a Comment