Java

How to Set JAVA_HOME Linux

Java is one of the most influential and popular programming languages. Originally released by Sun Microsystems in 1995, Java is a cross-platform language that is now a part of almost every aspect of technology. You will find Java everywhere – banking, financial services, big data, stock market, mobile (Android), and more. It easily ranks among the top programming languages and is likely to remain so for at least a decade.

In this guide, we’ll showcase one of the basic and crucial steps of configuring Java on your system – setting up the JAVA_HOME environment variable in Linux.

Prerequisites

Before diving deeper, let’s quickly refresh on various concepts and keywords.

JDK vs. JRE

If you’re interested in Java, you are probably already familiar with them. JDK stands for “Java Development Kit.” It contains the necessary tools and libraries to build and run (using JRE) Java apps. If you’re interested in learning or working on a Java project, JDK is the option to choose.

JRE stands for “Java Runtime Environment.” This package contains the tools and libraries needed to run a Java application. It’s a must-have to run any Java program on the system.

Note that JDK comes bundled with JRE by default. So, you don’t have to install JRE separately if you already have JDK installed.

Environment variables

In Linux, environment variables hold various system info available to apps. The info can be about how apps run on the environment, different system behaviors, etc.

Depending on the accessibility of the variables, we can divide them into 2 categories.

  • Local environment variables: These variables are set on a per-user basis. Only the specific user can use them in their sessions.
  • Global environment variables: These variables are accessible by everyone on the system.

JAVA_HOME

The JAVA_HOME is an environment variable. It holds the location of the Java binaries. Many applications rely on this environment variable to locate the Java binaries and libraries.

It can be set both on a local or global basis.

Setting up JAVA_HOME

Now that we understand what JAVA_HOME is, it’s time to learn how to set its value.

First, figure out the version of Java installed. This version number is often linked to the Java binary path.

$ java -version

If Java was installed globally, then the installation is likely stored at the following location.

$ cd /usr/lib/jvm

This directory contains the Java binaries and libraries. Check the content of the directory.

$ ls -lh

In my case, I have OpenJDK 11 installed on Ubuntu (more on installing the latest Java on Ubuntu). From the output, we can see entries like “default-java” and “java-1.11.0-openjdk-amd64” are symlinks of “java-11-openjdk-amd64”.

We’re going to set the value of JAVA_HOME to java-1.11.0-openjdk-amd64 (recommended).

$ export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

Verify the result.

$ echo $JAVA_HOME

Note that this environment variable will only last for the current shell session. Once restarted, you have to set the value once again manually. To solve this issue, most shells come with a configuration file that contains codes and commands the shell must run whenever it launches. In the case of bash, it’s called bashrc (for zsh, it’s zshrc, etc.).

Open the file in a text editor.

$ nano ~/.bashrc

Now, add the following line at the end of the file. It will mark the environment variable accessible to all shell sessions and the binaries available directly from the PATH variable.

$ export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

$ export PATH=$PATH:$JAVA_HOME/bin

Save the file and close the editor. To take the changes into effect, reload the bashrc file.

$ source ~/.bashrc

Verify the result.

$ echo $JAVA_HOME

Setting JAVA_HOME globally

The previous section demonstrates setting up JAVA_HOME on a single user account. It’s a good practice because each user may prefer different configurations. Some may even use a completely different Java version or Java flavor.

Bash comes with a global bashrc file that every shell session in the system must load, irrespective of the user. By declaring the location of JAVA_HOME there, we can make it available for all users in the system. Note that it’s not recommended and should be used only in specific situations.

Open up the global bashrc file in a text editor. Note that it requires sudo privilege to tweak this file.

$ sudo nano /etc/profile

Now, update the values of JAVA_HOME and PATH.

$ export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

$ export PATH=$PATH:$JAVA_HOME/bin

Save the file and close the editor. Reload the file into the bash shell to take the changes into effect.

$ source /etc/profile

Verify the result.

$ echo $JAVA_HOME

Final thoughts

In this guide, we’ve explored various concepts like environment variables and demonstrated how to set JAVA_HOME as a local or global environment variable. Numerous development apps like NetBeans, Eclipse, Maven, ANT, Apache Tomcat, Android Studio, and more depend on JAVA_HOME to function properly.

Note that if the variable was set globally, then the location must be accessible to all users in the system. Otherwise, it will cause many issues, leading to severe headaches. To solve file permission conflicts, Linux comes with a built-in tool: chown. Learn more about chown and how to use it.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Page was generated in 0.057765007019043