JAVA_HOME is set to the wrong directory

Hello fellow Ubuntu users,

I am trying to learn how to write modifications to Minecraft. In order to follow the tutorial I am using, I need to run a "gradlew". The command is as follows

./gradlew setupDecompWorkspace eclipse

This yields the following

ERROR: JAVA_HOME is set to an invalid directory: /usr/local/java/jdk1.8.0_20

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

Obviously I don't know what to do or even what this means. Any help is much appreciated.


Solution 1:

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

Solution 2:

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!)

Solution 3:

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/

Solution 4:

Try to edit /usr/bin/gradle

Comment the line "export JAVA_HOME=/usr/lib/jvm/default-java" with a #

this: #export JAVA_HOME=/usr/lib/jvm/default-java