Jump to content

How to Set JAVA_HOME in Linux


Recommended Posts

Setting up the JAVA_HOME is essential when you want to develop the Java-based applications on your Linux system. The JAVA_HOME points to the directory where you install JDK or Java Development Kit.

Hence, it helps the Java applications to locate the libraries and binaries to run appropriately. Furthermore, the JAVA_HOME variable provides JDK with access to every program that requires Java. Although you can configure it right after installing JDK or Java Runtime Environment (JRE), most beginners don’t know its correct approach. So, this quick guide will briefly explain the simplest way to set JAVA_HOME in Linux without hassle.

How to Set JAVA_HOME in Linux

First, navigate to where you installed Java in your system and copy its path. It is the most crucial step because you will set it as the value of the JAVA_HOME variable in the coming steps.

It is usually located in the “/usr/lib/java” directory. However, if you are operating Linux on a virtual machine, the installation directory can certainly be “/usr/lib/jvm”. Go to this directory and enter the “ls” command.

set-java-home-linux-01.png

As visible as in the previous image, our installation directory is as follows:

/usr/lib/jvm/java-11-openjdk-amd64

Open the configuration file of your shell using a text editor. The shell configuration file paths are different for all shells. The following are the paths for commonly used shells:

  1. For Bash: ~/.bashrc
  2. For Zsh: ~/.zshrc

For example, let’s use the following command:

nano ~/.bashrc

set-java-home-linux-02.png

The file will open in a new window. Go to the end of the file and append the following text:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Replace “/usr/lib/jvm/java-11-openjdk-amd64” with the path that you copied in the earlier step.

set-java-home-linux-03.png

Once you are done, exit the editor. Now, you can either restart the terminal or enter the command as follows:

source ~/.bashrc

set-java-home-linux-04.png

You now successfully set JAVA_HOME. To make sure of it, you can verify it using the following command:

echo $JAVA_HOME

set-java-home-linux-05.png

Conclusion

The JAVA_HOME environment variable is essential to every application that runs on Java. However, users need to know its configuration process, so we explained the way to set JAVA_HOME in Linux. The overall method was simple: you must first copy the Java installation path and then export it as the value of the JAVA_HOME variable in your shell’s configuration file.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...