Software & AppsOperating SystemLinux

Fixing ‘Java Command Not Found’ Error on Ubuntu

Ubuntu 15

If you’re a developer or system administrator working with Ubuntu, you might have encountered the error message java: command not found even after installing Java on your system. This article will guide you through several solutions to fix this problem.

Understanding the Problem

The error java: command not found usually occurs when the system cannot find the Java executable in the directories listed in the PATH environment variable. This can happen if Java is not installed, or if it’s installed but the PATH is not set correctly.

Solution 1: Update Alternatives

The first solution is to run the update-alternatives command. This command is used in Ubuntu to maintain symbolic links determining default commands.

sudo update-alternatives --config java

This command lists all the installed Java versions and lets you select the default one. If there are no installed versions, you will need to install Java. You can follow the instructions provided in this guide to install Java.

Solution 2: Check for Dangling Links

If you encounter the message update-alternatives: warning: /etc/alternatives/java is dangling while running the previous command, it means there is a broken symbolic link. You can delete the dangling link by executing:

sudo rm /etc/alternatives/java

After deleting the link, run the update-alternatives command again.

Solution 3: Manually Set Java Path

If the above solutions don’t work, you can manually set the Java path in the /etc/profile file. Open the file with a text editor:

sudo nano /etc/profile

And add the following lines:

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

Here, JAVA_HOME is an environment variable that points to the directory where Java is installed. PATH is an environment variable that lists directories to be searched for executable files. Save the file and apply the changes by running:

source /etc/profile

Solution 4: Install Default Java

Finally, if none of the above solutions work, you can install a default Java version. Run one of the following commands:

sudo apt install default-jre
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-8-jre-headless

Here, apt is the package handling utility in Ubuntu, install is the command to install a package, and default-jre, openjdk-11-jre-headless, and openjdk-8-jre-headless are different Java packages that you can install.

After installing Java, test the java command to see if the issue is resolved.

Conclusion

The java: command not found error can be frustrating, but it’s usually easy to fix by either updating alternatives, checking for dangling links, manually setting the Java path, or installing a default Java version. If you follow the steps in this guide, you should be able to get Java working on your Ubuntu system.

Why am I getting the `java: command not found` error on Ubuntu?

The java: command not found error occurs when the system cannot find the Java executable in the directories listed in the PATH environment variable.

How can I fix the `java: command not found` error?

There are several solutions to fix the java: command not found error on Ubuntu. You can try updating alternatives, checking for dangling links, manually setting the Java path, or installing a default Java version.

What is the `update-alternatives` command used for?

The update-alternatives command is used in Ubuntu to maintain symbolic links determining default commands. It allows you to select the default Java version from the installed versions.

How do I use the `update-alternatives` command to fix the error?

To use the update-alternatives command, open your terminal and run sudo update-alternatives --config java. This command lists all the installed Java versions and allows you to select the default one.

What should I do if I encounter a warning about a dangling link?

If you encounter the warning update-alternatives: warning: /etc/alternatives/java is dangling, it means there is a broken symbolic link. You can delete the dangling link by executing sudo rm /etc/alternatives/java and then run the update-alternatives command again.

How can I manually set the Java path?

If the previous solutions don’t work, you can manually set the Java path in the /etc/profile file. Open the file with a text editor using sudo nano /etc/profile and add the necessary lines to set the JAVA_HOME and PATH environment variables. Save the file and apply the changes by running source /etc/profile.

What should I do if none of the above solutions work?

If none of the above solutions work, you can try installing a default Java version by running one of the following commands: sudo apt install default-jre, sudo apt install openjdk-11-jre-headless, or sudo apt install openjdk-8-jre-headless. After installing Java, test the java command to check if the issue is resolved.

How can I verify if Java is installed correctly after fixing the error?

After implementing the solutions, you can verify if Java is installed correctly by running the java -version command in your terminal. It should display the installed Java version information if everything is set up properly.

Leave a Comment

Your email address will not be published. Required fields are marked *