Software & AppsOperating SystemLinux

How To properly set JAVA_HOME and fix “which java” pointing to the wrong version in Ubuntu

Ubuntu 18

In this article, we will guide you through the process of properly setting the JAVA_HOME environment variable and fixing the issue of the which java command pointing to the wrong version of Java in Ubuntu. This guide is intended for system administrators and developers who need to ensure that their system is using the correct version of Java.

Quick Answer

To properly set JAVA_HOME and fix the issue of which java pointing to the wrong version in Ubuntu, you need to set the JAVA_HOME environment variable to the root directory of your Java installation and update the PATH variable to include the bin directory of your Java installation. Additionally, you can use the update-alternatives command to set the default Java version if multiple versions are installed.

Understanding JAVA_HOME and PATH variables

Before we delve into the solution, it’s important to understand what JAVA_HOME and PATH are. JAVA_HOME is an environment variable which should point to the directory where Java is installed on your system. The PATH variable, on the other hand, is a system variable that your operating system uses to locate needed executables from the command line or terminal window.

Setting the JAVA_HOME Variable

To set the JAVA_HOME variable correctly, it needs to point to the root directory of your Java installation, not the java binary itself. For example, if your Java installation directory is /usr/lib/jvm/java-7-openjdk-i386/, you would set the JAVA_HOME variable in your ~/.bash_profile file as follows:

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386/

This export command sets the value of JAVA_HOME to the path of your Java installation directory.

Updating the PATH Variable

Next, you need to update your PATH variable to include the bin directory of your Java installation. This is necessary so that your system can locate the Java executables.

In your ~/.bash_profile, update the PATH variable as follows:

export PATH=$PATH:$JAVA_HOME/bin

This command appends the path to the Java bin directory to the existing PATH variable.

Applying the Changes

After updating the JAVA_HOME and PATH variables, you need to apply the changes. You can do this by running the following command:

source ~/.bash_profile

The source command reads and executes commands from the file specified as its argument, in this case, ~/.bash_profile.

Verifying the Changes

To verify that JAVA_HOME and PATH have been updated correctly, you can use the echo command:

echo $JAVA_HOME
echo $PATH

These commands should display the paths you set earlier.

Fixing “which java” pointing to the wrong version

If which java is still pointing to the wrong version, it means that there are multiple Java versions installed on your system and the wrong version is being prioritized.

You can use the update-alternatives command to set the default Java version:

sudo update-alternatives --config java

This command lists all installed Java versions and allows you to select the one to use as default.

Conclusion

In this article, we’ve covered how to set the JAVA_HOME variable, update the PATH variable, and fix the which java command pointing to the wrong version in Ubuntu. By following these steps, you should be able to ensure that your system is using the correct version of Java. If you still encounter issues, you may need to check your system’s configuration or consult the official Java documentation.

How do I check the current version of Java installed on my system?

To check the current version of Java installed on your system, you can use the following command in the terminal:

java -version

This command will display the version information of Java.

Can I have multiple versions of Java installed on my system?

Yes, it is possible to have multiple versions of Java installed on your system. However, you need to ensure that the correct version is being used by setting the JAVA_HOME and PATH variables appropriately.

How do I install a specific version of Java on Ubuntu?

To install a specific version of Java on Ubuntu, you can use the apt package manager. For example, to install Java 8, you can run the following command:

sudo apt install openjdk-8-jdk

Replace 8 with the desired version number.

Can I use a different shell other than Bash to set the JAVA_HOME and PATH variables?

Yes, you can use a different shell to set the JAVA_HOME and PATH variables. The process may vary depending on the shell you are using. For example, if you are using Zsh, you would edit the ~/.zshrc file instead of ~/.bash_profile. The concept remains the same; you need to set the JAVA_HOME variable to the Java installation directory and update the PATH variable to include the bin directory of Java.

How can I uninstall a specific version of Java on Ubuntu?

To uninstall a specific version of Java on Ubuntu, you can use the apt package manager. For example, to uninstall Java 8, you can run the following command:

sudo apt remove openjdk-8-jdk

Replace 8 with the version number you want to uninstall.

Leave a Comment

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