Gradle finds wrong JAVA_HOME even though it's correctly set

Turns out that the particular Gradle binary I downloaded from the Ubuntu 13.10 repository itself tries to export JAVA_HOME. Thanks to Lucas for suggesting this.

/usr/bin/gradle line 70:

export JAVA_HOME=/usr/lib/jvm/default-java

Commenting this line out solves the problem, and Gradle finds the correct path to the Java binary.

If you just download the binary from their website it does not have this problem, It's an issue with the Ubuntu repo version. There also seem to be some other issues with 13.10 version.


add a symbolic link

sudo ln -s /usr/lib/jvm/java-7-oracle /usr/lib/jvm/default-java

Solution is to make JAVA_HOME == dir above bin where javac lives as in

type javac

javac is /usr/bin/javac   # now check if its just a symlink

ls -la /usr/bin/javac 

/usr/bin/javac -> /etc/alternatives/javac   # its a symlink so check again

ls -la /etc/alternatives/javac  # now check if its just a symlink

/etc/alternatives/javac -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javac

OK so finally found the bin above actual javac so do this

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

above can be simplified and generalized to

which javac >/dev/null 2>&1 || die "ERROR: no 'javac' command could be found in your PATH"
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac)  )))