How to Fix the “java command not found” in Linux?

The Java Runtime Environment utility is used to build Java-based websites using the default libraries from the JRE in most Linux distributions. The Java utility is used by some distros (Fedora, Arch Linux) to develop Java-founded applications. While working with the Java language, users can encounter the “java command not found” error; the causes and solutions will be discussed in this article.

This post will cover the following topics to remove the error “java not found” in Linux:

Let’s start with discussing the java error.

Reason 1: Required Package ( JRE/java ) is Not Installed

Most Linux distributions do not have java Runtime Environment (JRE) installed after installing the operating system. JRE/java allows the developers to build Java-based applications, so for operating the JAVA language, we must have JRE in Linux. If the operating system does not have JRE, the below error will show up:

How to install Kali Linux in Window...

Error: Command “java not found”.

Let’s solve this error.

Solution: Add the Required Package ( JRE/java )

The solution for the error ”java not found” is to install the Java Runtime Environment or Java package that helps to build the Java application, and java uses the JRE package libraries to execute Java. To install the JRE in Linux, use the below-mentioned steps:

Step 1: Update the System Libraries

Before installing the JRE, it’s recommended to update the system libraries so the packages in the default repository are updated. To update, use:

$ sudo apt update

The JRE is updated to the latest version in the system repository.

Step 2: Install the Required ( JRE/java ) Package

Let’s install the JRE package to remove the “java not found” error. Use the below command utilities to install the JRE according to your operating system:

$ sudo apt install default-jre               #Debian-based distros.
$ sudo yum install java                      #CentOS/RHEL.
$ sudo pacman -sS java | grep jre            #Arch-based distros.

You can verify the installation of the java/JRE package using the following command:

$ java --version

The required JRE package is successfully installed in the system having version “11.0.17”; now, the “java not found” error will be removed.

Reason 2: Java PATH Environment Variable is Missing

After installing the JRE/java packages in the system, the error is still not removed, which means the java PATH environment variables are missing in your system. For adding the PATH variable, follow the below solution.

Solution: Add the java PATH Environment Variable

The java PATH environment can be added to its default “/etc/profile” file to remove this error. For that, follow the below-performed steps:

Step 1: Find the java PATH Variables Details  

To find the required details for the Java PATH variables, run the below-mentioned command:

$ sudo update-alternatives --config java

The output shows the absolute path that will be used to update the environment PATH variable. Now, copy the highlighted path address to use in the next step.

Step 2: Create a Code for Adding PATH Variable

Update the “JAVA_HOME” variable with the path variable we found in the previous step. The below code is shown to update the PATH variables in the default system file:

JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH

Step 3: Add/Update the PATH Variable in the 

After creating the PATH variable, now use the below command to open the “/etc/profile” file to add the PATH variable.

$ sudo gedit /etc/profile

After executing the command, the above file will open up. Add the PATH variable code (step 2 code) in the end as shown below and save the file:

Step 4: Reboot the System

Now, reboot the system to update the PATH variable in the system by executing this command:

$ reboot

That’s how you will get rid of the “java command not found error”.

Conclusion

The error “Command ‘java’ not found” in Linux can be removed by installing the missing required package (JRE or Java). After installing the required package, if the error is not removed, add the PATH environment variable in the “/etc/profile” file manually, as performed in the article, and the error will be removed. This post has explained the possible reasons and solutions for the error “java command not found”.