Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

java-home

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.

like image 937
Parag Avatar asked Aug 23 '12 06:08

Parag


People also ask

What is JAVA_HOME path in Linux?

$ export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64. $ export PATH=$PATH:$JAVA_HOME/bin. Save the file and close the editor. Reload the file into the bash shell to take the changes into effect. $ source /etc/profile.

What is the correct JAVA_HOME path?

Set JAVA_HOME: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1. 6.0_02.


2 Answers

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.

like image 69
Nu-ONE Avatar answered Oct 04 '22 02:10

Nu-ONE


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

like image 20
paulsm4 Avatar answered Oct 04 '22 02:10

paulsm4