JAVA_HOME is set to the wrong directory

Before running the command try entering:

export JAVA_HOME="path_to_java_home"

Where path_to_java_home is the folder where your bin/java is.

If java is properly installed you can find it's location, by using the command:

readlink -f $(which java)

Don't forget to remove bin/java from the end of the path while putting it into JAVA_HOME


To make it automatic on Linux/Ubuntu, add these lines at the end of your .bashrc:

JAVA_HOME=$(dirname $( readlink -f $(which java) ))
JAVA_HOME=$(realpath "$JAVA_HOME"/../)
export JAVA_HOME

This gets java executable, then climbs up a directory level

To designate the default java version/executable on Debian/Ubuntu, run this once:

sudo update-alternatives --config java

This builds up on previous answer by Andrew Onischuk (thanks!)


Get your Java path using the command

whereis java

The following is a possible output depending on your default Java version

java: /usr/bin/java /usr/share/java /usr/lib/jvm/jdk1.8.0_241/jre/bin/java

Maven need to pick the Java location (home) so you can export the Java path using the following command

export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_241/jre

This will allow Maven to pick the java version and location in the /etc/environment file

Optionally you can add this in the /etc/environment manually using the following command

sudo nano /etc/environment

and edit the JAVA_HOME=/usr/lib/jvm/jdk1.8.0_241/jre/