How to find path to java?

Simply do (in terminal):

update-alternatives --list java

And you'll get an output like this:

michael@NEXUS-TWO:/usr/lib/x86_64-linux-gnu/bamf$ update-alternatives --list java
/usr/bin/gij-5
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

The last line is the place your java is in.


You need to dig into symbolic links. Below is steps to get Java directory

Step 1:

$ whereis java
java: /usr/bin/java /etc/java /usr/share/java

That tells the command java resides in /usr/bin/java.

Step 2:

$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 2009-01-15 18:34 /usr/bin/java -> /etc/alternatives/java

So, now we know that /usr/bin/java is actually a symbolic link to /etc/alternatives/java.

Dig deeper using the same method above:

Step 3:

$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 31 2009-01-15 18:34 /etc/alternatives/java -> /usr/local/jre1.6.0_07/bin/java

So, thats the actual location of java: /usr/local/jre.....

You could still dig deeper to find other symbolic links.


Reference : where is java's home dir?


export JAVA_HOME=$(dirname $(dirname $(update-alternatives --list javac)))

To make this seemingly over done setting clearer, on my Ubuntu linux machine with open JDK 8 installed:

$ update-alternatives --list java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

$ update-alternatives --list javac
/usr/lib/jvm/java-8-openjdk-amd64/bin/javac

but what we need is the path to the directory containing bin of the JDK. So ask for the location of javac and then use dirname twice.

See man update-alternatives for more.