What is the correct path for JAVA_HOME on a Linux system that uses alternatives?

I didn't find a proper direct solution for the issue, so here is my workaround add the following to the bachrc

javapath=$( readlink --canonicalize /usr/bin/java)
removebin="/bin/java"
removejre="/jre"
javapath2=${javapath/$removebin/}
export JAVA_HOME=${javapath2/$removejre/}

Then do source to reload the JAVA_HOME whenever you change the java version using alternatives

source ~/.bashrc 

Explanation: What I have done is get the java classpath from the variable set by update-alternatives app and then remove the bin/java part from it and then assign it to the JAVA_HOME. This process happens at login to the system. if you alter the java version in the middle of a session you will have to reload the profile.


Good question - I use "alternatives" on Linux and everything "just works" - I never really had to think about it.

I believe this is the answer:

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

  • What is the correct target for the JAVA_HOME envrionment variable for a Linux OpenJDK Debian-based distribution?

1) "alternatives" sets the symlink to whatever your "real" Java is currently configured to

2) All you need to do is set $JAVA_HOME to the symlink

Tags:

Java

Java Home