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

2

13

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.

Chuckchuckfull answered 23/8, 2012 at 6:29 Comment(2)
I just looked on my CentOS 5.5 system: 1) I do not have $JAVA_HOME defined anywhere in my environment, 2) /etc/tomcat5/tomcat5.conf has this line: JAVA_HOME="/usr/lib/jvm/java", 3) /usr/lib/jvm/java is a symlink: java -> /etc/alternatives/java_sdk, 4) /etc/alternatives/java_sdk is also a symlink: java_sdk -> /usr/lib/jvm/java-1.4.2-gcj.Mcpherson
Yes, I have 'default-java' on Ubuntu instead of 'java' inside /usr/lib/jvm So, I guess the trick is to not worry about alternatives at all because there is one more layer between alternatives and Java.Chuckchuckfull
M
8

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:

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

Mcpherson answered 23/8, 2012 at 6:38 Comment(4)
On which distro is this? I'm on Debian, which uses alternatives, and I don't have a /usr/lib/jvm/default-javaWandie
Interesting answer. I first started this comment with an explanation of why, what you are suggesting will not work. But as I thought of it more I think I understand what you are suggesting. Are you saying that I should create a 'default-java' directory and symlink it to the Java I want to use. Following this I should update my alternatives system to point all Java related binaries to the ones in 'default-java/bin'. This will solve the problem by adding one more layer between alternatives and JavaChuckchuckfull
Thanks. I was a bit confused because I had messed around with my alternatives, and the java binaries were no longer going through default-javaChuckchuckfull
It seems to be JAVA_HOME=/usr/lib/jvm/java for redhat.Spadiceous
B
1

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.

Brotherson answered 8/1, 2021 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.