Ubuntu – How to determine JAVA_HOME on Debian/Ubuntu

debianjavaUbuntu

On Ubuntu it is possible to have multiple JVMs at the same time. The default one is selected with update-alternatives. But this does not set the JAVA_HOME environment variable, due to a debian policy.

I am writing a launcher script (bash), which starts a java application. This java application needs the JAVA_HOME environment variable. So how to get the path of the JVM which is currently selected by update-alternatives?

Best Answer

For the JRE, something like this should do the trick:

JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")