96

Is there any linux command I could use to find out JAVA_HOME directory? I've tried print out the environment variables ("env") but I can't find the directory.

willome
  • 3,062
  • 19
  • 32
Progress Programmer
  • 7,076
  • 14
  • 49
  • 54

11 Answers11

113

On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))

On Mac you can run $(dirname $(readlink $(which javac)))/java_home

I'm not sure about windows but I imagine where javac would get you pretty close

jsdevel
  • 1,539
  • 1
  • 12
  • 8
  • 7
    This is a great solution, but remember to use the canonical flag for readlink as Java can be set up with multiple links, so use `$(dirname $(dirname $(readlink -f $(which javac))))` on Linux instead – Matthew O'Riordan Aug 03 '14 at 12:22
  • Thanks @MatthewO'Riordan! I added the `-f` flag for the linux command. Note that `readlink` on mac is not the `GNU` version so I excluded `-f` for the mac version of the script. Mac users that have the `GNU` version of `readlink` installed via brew can use `-f`. – jsdevel Nov 14 '15 at 09:33
  • This is particularly nice for people who use /etc/alternatives to manage different java versions. – gilbertpilz Jan 05 '16 at 22:11
  • Worked fine on Mac OS – minhas23 Feb 26 '16 at 07:04
  • On OSX specifically, the following is preferred: `/usr/libexec/java_home -v1.8` for example to display JDK 1.8's home. – ikaerom Apr 24 '17 at 17:51
  • 2
    This is the best solution to find any home paths. I use it frequently for java, maven, and whatnot! Thank you! – Martin Majewski Oct 01 '18 at 10:31
  • I get this: "-bash: /usr/lib/jvm/java-8-openjdk-amd64: Is a directory" – Caleb Stanford Nov 03 '21 at 14:12
  • 2
    Ah, nevermind, the intention is clearer with an echo before it: `echo $(dirname $(dirname $(readlink -f $(which javac))))` – Caleb Stanford Nov 03 '21 at 14:14
80

Just another solution, this one's cross platform (uses java), and points you to the location of the jre.

java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'

Outputs all of java's current settings, and finds the one called java.home.

For windows, you can go with findstr instead of grep.

java -XshowSettings:properties -version 2>&1 | findstr "java.home"
Kıvılcım
  • 391
  • 3
  • 8
Parth Mehrotra
  • 2,712
  • 1
  • 23
  • 31
  • 3
    I've reseached a lot of ways to find JAVA_HOME and I think this is one of the best! Thanks for mentioning it! – drwatsoncode Apr 05 '17 at 01:54
  • Glad it helped! – Parth Mehrotra Apr 07 '17 at 01:37
  • 1
    It's probably worth mentioning that this started working with JDK1.7 (IIRC). Before that you would have needed to call: `jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));'`. In both cases, for 'cross-platform' aspects, it is recommendable to prepend a `/usr/bin/env`. – ikaerom Apr 24 '17 at 17:47
  • 2
    Holy moly this one works better than all the others, thanks! – Harvey Lin Jun 22 '18 at 00:22
  • 1
    This is the better answer. – dr_ Jul 18 '18 at 14:22
67

echo $JAVA_HOME will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.

If you try which java and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.

Liam M
  • 5,306
  • 4
  • 39
  • 55
AdamC
  • 16,087
  • 8
  • 51
  • 67
  • 13
    JAVA_HOME is no longer defined in Linux systems and is deprecated. – Peter Quiring Nov 03 '14 at 20:07
  • 3
    @PeterQuiring you should cite your source for that statement.. you are quite wrong. – harschware Jun 26 '15 at 00:02
  • 3
    @harschware When I install Ubuntu the JAVA_HOME is not set, even with the JDK installed. Also read /usr/share/doc/openjdk-8-jre-headless/JAVA_HOME and it says the JAVA_HOME should no longer be needed (legacy). – Peter Quiring Jun 27 '15 at 01:49
  • @PeterQuiring Thanks Peter, unfortunately I don't have easy access to that file. Perhaps there is an effort to deprecate it in Java 8, but the shear number of tools that rely on JAVA_HOME will mandate a need to continue to define the variable for many years yet... – harschware Jun 27 '15 at 19:41
  • @harschware It could be just an Ubuntu thing. Maybe other distros still define it. – Peter Quiring Jun 28 '15 at 20:12
  • JAVA_HOME should not be set, java knows where it is installed. if you need to find out you might use: java -XshowSettings:properties -version 2>&1 | grep "java.home" as Parth suggested below. – soloturn Apr 21 '20 at 04:20
  • Maven installation documentation step 1 says "Ensure JAVA_HOME environment variable is set" – Andy Feb 09 '21 at 16:56
35

I know this is late, but this command searches the /usr/ directory to find java for you

sudo find /usr/ -name *jdk

Results to

/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk

FYI, if you are on a Mac, currently JAVA_HOME is located at

/System/Library/Frameworks/JavaVM.framework/Home

cevaris
  • 5,671
  • 2
  • 49
  • 34
12

To show the value of an environment variable you use:

echo $VARIABLE

so in your case will be:

echo $JAVA_HOME

In case you don't have it setted, you can add in your .bashrc file:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

and it will dynamically change when you update your packages.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • Unsafe sed command: what if /usr/bin/java was a symlink to `/usr/bin/java-8/bin/java`? You'd get the output `/usr/-8/bin/java`. – GKFX Sep 08 '16 at 20:25
  • 1
    Fantastic Solution! if you want to use jdk as home instead of jre just tweak readlink -f /usr/bin/java | sed "s:jre/bin/java::" – cyberoblivion Jan 26 '17 at 15:18
10

If $JAVA_HOME is defined in your environment...

$ echo $JAVA_HOME
$ # I am not lucky...

You can guess it from the classes that are loaded.

$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]

This method ensures you find the correct jdk/jre used in case there are multiple installations.

Or using strace:

$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
dnozay
  • 23,846
  • 6
  • 82
  • 104
3

On the Terminal, type:

echo "$JAVA_HOME"

If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
2

Did you set your JAVA_HOME

  • Korn and bash shells:export JAVA_HOME=jdk-install-dir
  • Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
  • C shell:setenv JAVA_HOME jdk-install-dir
Nizar Grira
  • 743
  • 2
  • 7
  • 21
1

Here's an improvement, grabbing just the directory to stdout:

java -XshowSettings:properties -version 2>&1 \
   | sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'
  • 2
    A bit shorter, though with three forks instead of two, is the following: `: ${JAVA_HOME:=$(/usr/bin/env java -XshowSettings:properties -version 2>&1 | grep "java.home" | cut -d"=" -f2)}` – ikaerom Apr 24 '17 at 17:49
  • If one needs to export the path directly: `export $(/usr/bin/env java -XshowSettings:properties -version 2>&1 | grep "java.home" | sed -e 's/java.home/JAVA_HOME/;s/ //g;')` – ikaerom Apr 24 '17 at 18:04
1

You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
   2           java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3           /usr/java/jdk-17.0.2/bin/java

Enter to keep the current selection[+], or type selection number: 

from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2

-1

http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment

If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.

Douglas G. Allen
  • 2,203
  • 21
  • 20