When you try to run a Java program after updating your system's version of Java, you are likely to receive an error message such as the following:
mvn install
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
The source of the problem is probably the setting of your JAVA_HOME
environment variable. This article shows you how to fix this problem.
The reason for the error message
Java programs need a Java Runtime Environment (JRE) to run. When you program with Java, the JRE is part of the Java Development Kit (JDK). The meanings of these terms follow:
- Java Runtime Environment (JRE): Java source code is compiled and converted to Java bytecode. If you want to run this bytecode on your platform, you need the JRE to load and interpret the bytecode.
- Java Development Kit (JDK): This includes a JRE along with a collection of software development tools and libraries to create Java programs that can run on the platform using the JRE.
The importance of JAVA_HOME
Your shell finds the proper Java environment through an environment variable called JAVA_HOME
. It could point to either a JDK or a JRE. As the error message shown earlier indicates, you should point to your JDK if you have a JDK. When non-developers run Java, they don't need the entire JDK, so they can download a JRE and point JAVA_HOME
to that. You can learn more about the Java platform components in the article, What is the Java Runtime Environment.
When you upgrade Java, the system installs a new JDK, and you need to update JAVA_HOME
to point to the new location.
Environment variables used in the shell are generally defined in a start-up file. This file is located in your home directory and is called .bashrc
if you use the Bash shell that is the default on Linux. Note the initial dot in the name .bashrc
: this dot means that the file doesn't normally turn up when you display the contents of the directory.
The .bashrc
file is a script that runs when a user logs in. The file itself contains a series of commands to configure the terminal session.
Check your .bashrc
file to see whether it defines JAVA_HOME
and what value is assigned to the environment variable. For instance, you can execute this command in the terminal:
$ more ~/.bashrc
The line defining JAVA_HOME
in the output is as follows:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.4.1.1–1.fc35.x86_64
You have to make sure that JAVA_HOME
is set correctly, with version details.
Choosing a JDK
In the terminal on a Linux system, run the following command:
$ sudo alternatives --config java
The output displays the versions of Java currently on the system, as shown in Figure 1. The command also issues a prompt allowing you to choose a version.
The version you choose (3 in the figure) contains, within parentheses, a full pathname that you have to assign to your JAVA_HOME
environment variable in your .bashrc
file. After entering the number that you want to use, press the Enter key to finish the command.
Updating .bashrc
The next step is to check the value of JAVA_HOME
(or add it, if it is not already present) in the .bashrc
file. If .bashrc
doesn't match the pathname you choose, you can copy the pathname from the terminal display and paste it into .bashrc
as I'll show shortly.
Figure 2 highlights the JDK path I selected in Figure 1.
Figure 3 highlights the JDK path in the JAVA_HOME
setting in my current .bashrc
file.
The paths are different, so I need to update the environment variable in the .bashrc
file to match the path offered by the system. Specifically, I need to change the JDK version in the path from openjdk-17.0.4.1.1–1.fc35.x86_64
to openjdk-17.0.5.0.8–2.fc35.x86_64
.
When you save your changes, .bashrc
looks like Figure 4.
Updating the shell
To reflect the changes in your shell, either close and launch the terminal again or use the following command:
$ source ~/.bashrc
In this way, you can easily update your JAVA_HOME
environment variable and continue coding in Java.