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

up vote 68 down vote accepted

Type in terminal,


Display JAVA_HOME variable path.


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

This will differ according to your JDK type & version

For displaying follow first command.

Follow this post for using different JDK's or switch between JDK's

  • 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 3 '14 at 6: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 3 '14 at 6:31
  • you should set it in /etc/environment – keiki May 3 '14 at 6: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 3 '14 at 15:29
  • 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 3 '14 at 15:49

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"));'

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.

  • That was a lot to go through to figure out i needed to go one directory deeper, but thanks. – cnizzardini Jan 25 '17 at 3:46

Another portable options is to extract the absolute path of the JDK from javac:

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

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

$ type -p javac

$ readlink -f /usr/bin/javac

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

$ dirname /usr/lib/jvm/java-8-oracle/bin/
  • 1
    one can use type -p javac|xargs readlink -f|xargs dirname|xargs dirname (without the export part) to know the directory without setting it. – Mina Michael Oct 2 '17 at 7:23

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')"

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

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$//')"

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

For Java 9 and later:

This answer 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:


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.