What is the correct path for JAVA_HOME on a Linux system that uses alternatives?
Question
Determining the correct path for JAVA_HOME
is a bit complex on an Ubuntu system because it uses alternatives. On my machine this is how alternatives creates at least two levels of indirection before it gets to the actual java or javac.
usr/bin/javac -> /etc/alternatives/
/etc/alternatives/java -> /usr/lib/jvm/jdk1.7/bin/javac
If I set JAVA_HOME
to /usr/lib/jvm/jdk1.7
, then it is possible that my system java might become inconsistent with the java pointed to by JAVA_HOME
, if I update alternatives to use another java.
My question is, what is the correct value for JAVA_HOME
on a system that uses alternatives. I am inclined to think that JAVA_HOME
should be set to /usr
This way TOMCAT or any other software that uses it, will append 'bin' to JAVA_HOME
and find all the executables it needs.
Is this the correct value for JAVA_HOME
on systems that use alternatives. Do most software use JAVA_HOME
only to locate the executables, or would they use the value to locate other artifacts (such as the security policy fil, etc) that come bundled with the JDK ? If the former is true, then I feel we can use /usr
for JAVA_HOME
, but if the latter is true, then I think the only way to use JAVA_HOME
correctly is by sacrificing the alternatives functionality.
Solution
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
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
OTHER TIPS
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.