On Ubuntu it is possible to have multiple JVMs at the same time. The default one is selected with update-alternatives
. But this does not set the JAVA_HOME
environment variable, due to a debian policy.
I am writing a launcher script (bash), which starts a java application. This java application needs the JAVA_HOME
environment variable. So how to get the path of the JVM which is currently selected by update-alternatives
?
For the JRE, something like this should do the trick:
danadam's solution can easily be adopted to retrieve the JDK (i.e. not JRE) path as required:
export JAVA_HOME=$(dirname $(dirname $(readlink -f /usr/bin/java)))
In
.bashrc
was handy for me.So, you're saying that this command does nothing for you?
I installed java with
and then to find the location
As an extension of danadams answer:
First of all, install the 2nd Java JRE as the 3rd java option, with priority of "3":
Then, you can list them:
You can set the alternative by hand , using this:
Then, your script can set it on the fly, like so:
This better illustrates what the 'sed' command is doing. Although you still need to set the links for javaw and javac, etc, as well, each done separately.
A while ago I created a tutorial on the Ubuntu forum on how to install the latest JRE/JDK from the Java website. It also covers on how to enable it system-wide, by adding the JRE/JDK location to the PATH variable. If you like, you can also add JAVA_HOME to the script, mentioned at the end of the topic.
Check it out: http://ubuntuforums.org/showthread.php?t=1437100
If java is configured with
update-alternatives
or was added to your PATH variable manually, then no hardcoded"/usr/bin/java"
is needed. I use this solution in my .bashrc:export JAVA_HOME=$(readlink -m $(which java)/../..)