What is the difference between JAVA_HOME and update-alternatives?
Alternatives
Alternatives is a tool that will manage the locations of the installed software using links under the control of the alternatives
tool.
These links are ultimately managed under /etc/alternatives
with intermediate links created under a directory in $PATH
, typically /usr/bin
.
Example
$ ls -l /usr/bin/java
lrwxrwxrwx. 1 root root 22 Feb 24 17:36 /usr/bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 Feb 24 17:36 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.60-2.4.5.0.fc19.x86_64/jre/bin/java
$JAVA_HOME
$JAVA_HOME
is where software can be told to look through the use of an environment variable. Adding it to the $PATH
simply adds the executables present in $JAVA_HOME/bin
to your $PATH
. This is sometimes necessary for certain applications.
The 2 mechanisms are related but can be used together or independent of each other, it really depends on the Java application which mechanism is preferable.
What I do
I typically use $JAVA_HOME
for some GUI applications, but in general use it only for server installations that make use of Jetty, Tomcat, or JBOSS, for example.
For these installations I'll still use alternatives
to manage the Java installations prior to setting the $JAVA_HOME
. I like doing it this way in cases where I might need to have multiple installations of Java.
Alternatives does allow you to have certain tools use one installation of Java while other tools use a completely different one.
References
- 5 Minute Guide to using the alternatives Command on Fedora/CentOS/RHEL
- Using 'Alternatives' in Linux to use a different Java package
Related videos on Youtube
Sudhir Singh Khanger
Updated on September 18, 2022Comments
-
Sudhir Singh Khanger almost 2 years
I have been trying to figure out the best way to run OpenJDK Java Runtime as default Java for my Fedora box and use Oracle JDK 6 for Android development namely for running Android SDK Manager, Android Studio and Eclipse from Android Bundle.
I installed OpenJDK Java Runtime from the Fedora repository which has setup alternatives as follow.
[donnie@fedora ~]$ alternatives --list | grep java jre_openjdk auto /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64/jre jre_1.7.0 auto /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64/jre java auto /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64/jre/bin/java libjavaplugin.so.x86_64 auto /usr/lib64/IcedTeaPlugin.so
I have installed the Oracle JDK 6 using the rpm provided by Oracle.
I could make Android Bundle and Studio make use of JAVA_HOME to run under Oracle JDK by sticking following in
.bashrc
.export JAVA_HOME=/usr/java/jdk1.6.0_45/ export PATH=$JAVA_HOME/bin:$PATH
I noticed that Chrome still uses OpenJDK (as I still need to link the plugin).
What are the difference between JAVA_HOME and using alternatives?
-
Sudhir Singh Khanger about 10 yearsThe giveaway is that with alternatives I can use different components of Java. For example, Alternatives can set
java
andjavac
from different JDKs on the other handJAVA_HOME=JAVALOCATION
would pointjava
,javac
, andjavaws
tojava
,javac
andjavaws
inJAVALOCATION
, respectively. -
Sudhir Singh Khanger about 10 yearsThe reason I asked this question was how can I use OpenJDK as default Java Runtime and make Android Studio, Eclipse and SDK Manager use Oracle JDK. I can run those three programs under Oracle JDK by hard editing their startup scripts which might get broken if they are updated. I was looking for something more generic like
STUDIO_JDK
, that I can add to~/.profile
or~/.bashrc
, that is used by Android Studio for picking up JDK but I think Android Bundle (Eclipse and SDK Manager) won't honor that. -
slm about 10 years@donniezazen - yeah
STUDIO_JDK
is a custom thing. I would use alternatives for those tools and setJAVA_HOME
to the path under/usr/bin
which is managed byalternatives
. When upgrades are performed, changingalternatives
is all that's required. -
Sudhir Singh Khanger about 10 yearsI am not sure what you are proposing. Point both
alternatives
andJAVA_HOME
to say/usr/bin/java
. Also it seems likeJAVA_HOME
overridesalternatives
. -
slm about 10 years@donniezazen - yes use alternatives to manage your JAVA installs and then use
JAVA_HOME
, pointing it to/usr/bin
. You're correct,JAVA_HOME
can operate independently fromalternatives
. -
Sudhir Singh Khanger about 10 yearsWhat are we trying to accomplish by pointing
JAVA_HOME
to/usr/bin
?/usr/bin
contains java binaries managed byalternatives
.JAVA_HOME
would be redundant in the case you are suggesting. And eitherOpenJDK
orOracle JDK
will become system default. Whereas I want OpenJDK as default and Oracle JDK for Android development. -
slm about 10 years@donniezazen - yes but some applications will only utilize Java through
JAVA_HOME
. Often times this is app servers such as JBoss or Jetty. They have stop/start service scripts that needJAVA_HOME
defined. GUI apps too will often times needJAVA_HOME
. It's hit or miss which fall into this category so it's often easier to just define it up front for the box. -
Sudhir Singh Khanger about 10 yearsThanks @slm you have been very helpful. It seems like there is no generic way to achieve what i want to do. I think i will go ahead and use OpenJDK's Icedtea for chrome for security and Oracle JDK as system default as Android development takes precedence.