How to permanently set JAVA_HOME on Ubuntu

featured image

If a project needs the JAVA_HOME variable pointing to a particular value and this requirement is not met on our environment, we will get errors while trying to build that project, e.g. the “Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set” error. Instead of exporting the JAVA_HOME value manually we can easily set the variable permanently with a custom script.

Find which Java versions are installed on your machine

First of all, I’m working on Ubuntu 20.04 LTS and all commands form this article were executed on the following release:

Furthermore, I use the OpenJDK for Java 14:

We can determine where Java was installed in the two ways described below.

Explore paths manually

Let’s start with the basic whereis command which we will use to locate the binary files for java:

As a result, we can see three different locations. We need to list content of the /usr/bin/java directory for more information. In my case I have a symbolic link pointing to /etc/alternatives/java from where I can confirm that the third location is the right one (/usr/lib/jvm/java-14-openjdk-amd64/bin/java):

Display information about the symbolic links

We can also use the update-alternatives command to directly list information about the java locations on our system:

Once we know to which location we want to point JAVA_HOME, we can check its current value and set it either permanently or temporarily.

Verify the current value

To see the content hold by the variable currently, run the following command in the terminal:

The output can be either empty or point to a value that is not compliant with requirements of the project we are trying to build.

Set the new value for JAVA_HOME permanently

As we can read in the Ubuntu documentation on environment variables:

A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment. An alternative is to create a file for the purpose in the /etc/profile.d directory.

https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables

Since we want the variable to hold its value despite restarting the system and for all users, we will create he jdk_home.sh script in the /etc/profile.d/ directory:

If you’re using the nano editor, hit the Shift+Insert shortcut to paste the following lines to the jdk_home.sh file (use the JAVA_HOME value correct for your system):

Next, hit the Ctrl+O shortcut followed by clicking Enter to save the changes and Ctrl+X to exit the file. The operation may fail if your user doesn’t have permission to modify the /etc content. However, running the nano editor in the sudo mode should solve this problem.

To verify that the file was modified correctly you can use the cat command to print its content:

Logout and login back to the system. Verify that the JAVA_HOME value is still correct:

Thanks to this flexible approach we can easily modify the variable by editing our custom script. Our system will execute it automatically on every restart – we don’t need to modify internal system configuration. Remember that:

While /etc/profile is often suggested for setting environment variables system-wide, it is a configuration file of the base-files package, so it’s not appropriate to edit that file directly. Use a file in /etc/profile.d instead

https://help.ubuntu.com/community/EnvironmentVariables#Other_files-1

Set the value until the next restart

We can temporarily set the JAVA_HOME in a bash shell until the next system restart with this command:

More on setting the JAVA_HOME variable

Photo by Sofia Garza from Pexels

Leave a Reply

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