91

How to find my current JAVA_HOME in ubuntu? I have to set java_home path when installing maven.

10 Answers10

131

To display JAVA_HOME variable path, type in terminal:

echo $JAVA_HOME

If nothing appears then set it with this:

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

This will differ according to your JDK type and version.

For displaying it again, follow the first command.

Follow JREs from different vendors on the same system, for using different JDK's or switch between JDK's.

Eliah Kagan
  • 117,780
Deepen
  • 4,049
  • 4
  • 22
  • 32
  • It gives "/usr/lib/jvm/java-6-sun". But I have installed java 7. When I check it using "java -version" it gives java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) Server VM (build 24.45-b08, mixed mode) – Samitha Chathuranga May 03 '14 at 06:29
  • 2
    then execute second command for setting JAVA_HOME variable. NOTE:JAVA_HOME doesn't make jdk default, it just makes JAVA_HOME variable set to a path & if you want to use different jdk installed on same machine then check my answer, I have edited it. – Deepen May 03 '14 at 06:31
  • you should set it in /etc/environment – keiki May 03 '14 at 06:40
  • @Jax-L But now when I give echo JAVA_HOME it just displays as "JAVA_HOME". The path I gave is not displaying...??? – Samitha Chathuranga May 03 '14 at 15:29
  • read it properly its echo $JAVA_HOME. You forgot dollar($) sign. – Deepen May 03 '14 at 15:33
  • @otakun85 setting in etc/environment does the same thing done by export command no? – Samitha Chathuranga May 03 '14 at 15:42
  • 2
    export works only until you restart. Or you add export to the .bashrc login script. But the correct way to set such environment variables is in /etc/environment – keiki May 03 '14 at 15:49
  • no, they aren't same, but can also set by editing /etc/environment file. Yes, they are same until you exit your session. Better set it in /etc/environment, you will find JAVA_HOME on reboot – Deepen May 03 '14 at 15:51
  • so does setting in etc/environment makes it permanent??? – Samitha Chathuranga May 03 '14 at 15:57
43

If you have JDK 1.6 (corresponding to Java 6) or a newer version installed, you should have a program named jrunscript in your PATH. You can use this to find the corresponding JAVA_HOME. Example:

$ jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));'
/opt/local/jdk1.7.0_76/jre

You could set the environment variable like this:

$ export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"

Note that the JRE doesn't include jrunscript, so this will only work if you install the JDK, not just the JRE.

28

Another portable options is to extract the absolute path of the JRE from java:

export JAVA_HOME=`type -p java|xargs readlink -f|xargs dirname|xargs dirname`

The absolute java path is passed to dirname twice to remove /bin/java from the end. Complete extraction of the directory goes as follows:

$ type -p java
/usr/bin/java

$ readlink -f /usr/bin/java
/usr/lib/jvm/java-8-oracle/bin/java

$ dirname /usr/lib/jvm/java-8-oracle/bin/java
/usr/lib/jvm/java-8-oracle/bin/

$ dirname /usr/lib/jvm/java-8-oracle/bin/
/usr/lib/jvm/java-8-oracle/
rzymek
  • 444
  • 4
  • 5
12

Just run a command

 sudo update-alternatives --config java

It will give something like

Es gibt nur eine Alternative in Link-Gruppe java (die /usr/bin/java bereitstellt): /usr/lib/jvm/java-8-oracle/jre/bin/java

From this you have /usr/lib/jvm/java-8-oracle/ as java home. You may now export it to JAVA_HOME variable

export JAVA_HOME=/usr/lib/jvm/java-8-oracle/

Now echo $JAVA_HOME show it

9

To take into account the update-alternatives mechanism:

$ update-alternatives --query java | grep 'Value: ' | grep -o '/.*/jre'

You could set the environment variable like this:

$ export JAVA_HOME="$(update-alternatives --query java | grep 'Value: ' | grep -o '/.*/jre')"
  • 1
    This doesn't quite give the JAVA_HOME as it will still have the trailing /jre. You might try export JAVA_HOME="$(update-alternatives --query java | grep 'Value: ' | sed -e 's/Value: //;s?/jre/bin/java??;')" – James Brusey Jul 25 '19 at 04:08
3

I use this in Ubuntu LTS (14.04 / 16.04):

[ -L /etc/alternatives/java ] && 
  export JAVA_HOME="$(readlink -f /etc/alternatives/java | sed -e 's/\/jre\/bin\/java$//')"
ggrandes
  • 163
3

For Java 9 and later:

This answer https://askubuntu.com/a/657468/963 uses the enclosed Nashorn JavaScript engine Nashorn to print out the java.home system property. Nashorn is being deprecated so an alternative is to use jshell introduced in Java 9.

echo 'System.out.println(java.lang.System.getProperty("java.home"));' | jshell  -

which on my Ubuntu 18.10 system prints out:

/usr/lib/jvm/java-11-openjdk-amd64
1

Set Java environment variables

The PPA also contains a package to automatically set Java environment variables, just run command:

sudo apt install oracle-java8-set-default

From this article: Install Oracle Java 8 / 9 in Ubuntu 16.04, Linux Mint 18

user2682025
  • 133
  • 6
1

to get JAVA_HOME:

update-alternatives --query java | grep 'Value: ' | sed 's/Value: \(.*\)\/bin\/java/\1/'

e.g. returns </opt/Oracle_Java/jre1.8.0_202>

if </etc/alternatives/java> points to

</opt/Oracle_Java/jre1.8.0_202/bin/java>

to set JAVA_HOME:

export JAVA_HOME="$(update-alternatives --query java | grep 'Value: ' | sed 's/Value: \(.*\)\/bin\/java/\1/')"
  • You can use a different separation character for sed expressions to make it simpler to read, for example an arobase : ... | sed 's@Value: \(.*\)/bin/java@\1@' (so you don't need to escape the slashes of the path). But it looks unusual for newcomers. – Lenormju Jul 21 '22 at 14:47
  • 1
    @Lenormju good point and thx for reminding, but with the @ it looks really weird to me and I would have to visually solve one more problem of already complicated stuff as you mentioned similarly. but ... | sed 's|Value: \(.*\)/bin/java|\1|' could be something I personally would choose (if it works - I did not test it) if it would seem more appropriate. – Andreas Covidiot Jul 21 '22 at 20:53
0

For versions that support -XshowSettings:properties:

While alternatives may remain popular I mostly use jenv and occasionally sdkman, which all introduce a level of indirection; if you are happy to ask java itself:

java -XshowSettings:properties -version |& grep -Po '(?<=java.home = )(.*)'

This is not available in Java 6:

docker run --rm openjdk:6 java -XshowSettings:properties -version
Unrecognized option: -XshowSettings:properties
Could not create the Java virtual machine.

It is available since Java 7:

 dk run --rm openjdk:7 bash -c "java -XshowSettings:properties -version |& grep -Po '(?<=java.home = |java.home=)(.*)'"
/usr/lib/jvm/java-7-openjdk-armhf/jre