106

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?

Witek
  • 1,433
  • 3
  • 14
  • 16

8 Answers8

148

For the JRE, something like this should do the trick:

JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
danadam
  • 1,646
  • 1
  • 10
  • 5
  • 18
    I needed the home of the JDK instead of the JRE, but I got this using: JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:bin/javac::") Thank You! – Witek May 22 '10 at 10:35
  • when I tried the command `echo $(readlink -f /usr/bin/java | sed "s:bin/java::")`, the output was `/usr/lib/jvm/java-7-oracle/jre/` and not `/usr/lib/jvm/java-7-oracle/` – Sumit Ramteke Jan 31 '14 at 05:20
  • Code given in [**rsaddey**](http://serverfault.com/questions/143786/how-to-determine-java-home-on-debian-ubuntu#276221) works correctly – Sumit Ramteke Jan 31 '14 at 05:28
  • You are a scholar and a gentleman. – rjurney Dec 04 '20 at 23:09
  • If someone is curious how `sed` works here: It replaces `bin/java` with an empty string. (Here `:` is used for `sed` separator instead of the usual `/`) – Mohammad Banisaeid Aug 01 '21 at 13:58
51

danadam's solution can easily be adopted to retrieve the JDK (i.e. not JRE) path as required:

JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
  • Looks for javac Java compiler (instead of java) included in JDK (but not JRE).
  • Has no trailing / (stripped off by sed s:/bin... instead of s:bin...)
rsaddey
  • 511
  • 4
  • 2
15

export JAVA_HOME=$(dirname $(dirname $(readlink -f /usr/bin/java)))

In .bashrc was handy for me.

jscott
  • 24,204
  • 8
  • 77
  • 99
David
  • 151
  • 1
  • 2
10

So, you're saying that this command does nothing for you?

sudo update-alternatives --config java 
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
djangofan
  • 4,172
  • 10
  • 45
  • 59
  • 2
    Is that suppose to set your JAVA_HOME? I've found that update-java-alternatives is a better way to update Java as update-alternatives doesn't update all Java related alternatives. See http://askubuntu.com/questions/141791/ – James McMahon Jul 30 '12 at 13:56
  • True, but I have encountered instances of Linux that had update-alternatives but did not have update-java-alternatives installed. – djangofan Jun 29 '14 at 17:16
3

I installed java with

sudo apt-get install openjdk-7-jre-headless

and then to find the location

ls -al /etc/alternatives/java
prule
  • 141
  • 3
2

As an extension of danadams answer:

First of all, install the 2nd Java JRE as the 3rd java option, with priority of "3":

sudo alternatives --install /usr/lib/jvm/jre jre /opt/IBM/java/jre/bin/java 3

Then, you can list them:

update-alternatives --list java

You can set the alternative by hand , using this:

sudo alternatives --config java /opt/IBM/java/jre/bin/java

Then, your script can set it on the fly, like so:

sudo alternatives --set java /opt/IBM/java/jre/bin/java
JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

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.

djangofan
  • 4,172
  • 10
  • 45
  • 59
1

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

aardbol
  • 1,463
  • 4
  • 17
  • 25
1

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)/../..)

Freddy
  • 1,999
  • 5
  • 12