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.
First of all, I’m working on Ubuntu 20.04 LTS and all commands form this article were executed on the following release:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
Furthermore, I use the OpenJDK for Java 14:
$ java --version
openjdk 14.0.1 2020-04-14
We can determine where Java was installed in the two ways described below.
Let’s start with the basic whereis command which we will use to locate the binary files for java
:
$ whereis -b java
java: /usr/bin/java /usr/share/java /usr/lib/jvm/java-14-openjdk-amd64/bin/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
):
$ ls -la /usr/bin/java
lrwxrwxrwx … /usr/bin/java -> /etc/alternatives/java
$ ls -la /etc/alternatives/java
lrwxrwxrwx … /etc/alternatives/java -> /usr/lib/jvm/java-14-openjdk-amd64/bin/java
We can also use the update-alternatives command to directly list information about the java locations on our system:
$ update-alternatives --list java
/usr/lib/jvm/java-14-openjdk-amd64/bin/java
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.
To see the content hold by the variable currently, run the following command in the terminal:
$ echo $JAVA_HOME
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.
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
https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables/etc/environment
. An alternative is to create a file for the purpose in the/etc/profile.d
directory.
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:
$ sudo nano /etc/profile.d/jdk_home.sh
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):
export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
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:
$ cat /etc/profile.d/jdk_home.sh
export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
Logout and login back to the system. Verify that the JAVA_HOME value is still correct:
$ echo $JAVA_HOME
/usr/lib/jvm/java-14-openjdk-amd64
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
https://help.ubuntu.com/community/EnvironmentVariables#Other_files-1/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
We can temporarily set the JAVA_HOME in a bash shell until the next system restart with this command:
$ export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
$ export PATH=$PATH:$JAVA_HOME/bin
Photo by Sofia Garza from Pexels
Spring Security allows us to use role-based control to restrict access to API resources. However,…
A custom annotation in Spring Boot tests is an easy and flexible way to provide…
Delegating user management to Keycloak allows us to better focus on meeting the business needs…
Swagger offers various methods to authorize requests to our Keycloak secured API. I'll show you…
Configuring our Spring Boot API to use Keycloak as an authentication and authorization server can…
Keycloak provides simple integration with Spring applications. As a result, we can easily configure our…