Run /usr/libexec/java_home
in a terminal. It will return the path that corresponds to $JAVA_HOME for your Mac's default Java installation. Attach /jre
to this path to get the Java runtime environment (JRE) path.
Example:
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home
JRE would be /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre
To verify:
$ ls -al /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre
total 552
drwxr-xr-x 10 root wheel 340 Apr 2 2019 .
drwxr-xr-x 15 root wheel 510 Apr 2 2019 ..
-r--r--r-- 1 root wheel 3244 Apr 2 2019 COPYRIGHT
-r--r--r-- 1 root wheel 44 Apr 2 2019 LICENSE
-r--r--r-- 1 root wheel 46 Apr 2 2019 README
-rw-r--r-- 1 root wheel 112748 Mar 14 2019 THIRDPARTYLICENSEREADME-JAVAFX.txt
-r--r--r-- 1 root wheel 149725 Apr 2 2019 THIRDPARTYLICENSEREADME.txt
-r--r--r-- 1 root wheel 955 Apr 2 2019 Welcome.html
drwxr-xr-x 13 root wheel 442 Apr 2 2019 bin
drwxr-xr-x 98 root wheel 3332 Apr 2 2019 lib
This also works with any OpenJDK installation:
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
Versions
If you have multiple JREs or JDKs installed, you can obtain $JAVA_HOME for a specific version using the -v
option:
$ % /usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
Tipp
To set the JAVA_HOME environment variable, use
$ export JAVA_HOME=`/usr/libexec/java_home`
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home