Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?
With the Java optional package or Oracle JDK installed,
adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.
export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"
Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
for the above to work
Update: added -v flag based on Jilles van Gurp response.
What should I set JAVA_HOME environment variable on macOS X 10.6?
I just set JAVA_HOME
to the output of that command, which should give you the Java path specified in your Java preferences. Here's a snippet from my .bashrc
file, which sets this variable:
export JAVA_HOME=$(/usr/libexec/java_home)
I haven't experienced any problems with that technique.
Occasionally I do have to change the value of JAVA_HOME
to an earlier version of Java. For example, one program I'm maintaining requires 32-bit Java 5 on OS X, so when using that program, I set JAVA_HOME
by running:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.5)
For those of you who don't have java_home
in your path add it like this.
sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home /usr/libexec/java_home
References:
Oracle explains the java_home command
An article for configuring the JDK in Spring Tool Suite (Eclipse
2019) on MacOS
How to set JAVA_HOME environment variable on macOS?
in .bash_profile:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.xx/Contents/Home
Default directory of Java 7 on Mountain Lion
Should be:
/Library/Java/JavaVirtualMachines
And don't forget, you may not be seeing it because the LIB folder is hidden.... with every update of OSX So use:
chflags nohidden ~/Library/
to unhide.
Permission Error When Exporting to $JAVA_HOME on MacOS Big Sur
The $(foo)
bit means, loosely, "run foo
as a program, then insert its output here and go on as if I had typed it", which is not what you want here. Just do
export JAVA_HOME=/Library/Java/...
The $()
bit is useful when you use the Mac's Java selection mechanism, and run e.g.
export JAVA_HOME=$(/usr/libexec/java_home -v16)
In that case, you're running a program, and setting JAVA_HOME
to the output of that program.
java_home issue on macbook 12.3.1 Monterey (M1)
Unfortunately, java_home
only looks for runtimes in 2 locations:
$HOME/Library/Java/JavaVirtualMachines
/Library/Java/JavaVirtualMachines
A symlink works but will need to be kept up-to-date as homebrew upgrades:
$ ln -sf /opt/homebrew/Cellar/openjdk@11/11.0.15/libexec/openjdk.jdk ~/Library/Java/JavaVirtualMachines/openjdk11
It then appears in the list in its original location:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
18.0.1.1 (arm64) "Homebrew" - "OpenJDK 18.0.1.1" /opt/homebrew/Cellar/openjdk/18.0.1.1/libexec/openjdk.jdk/Contents/Home
11.0.15 (arm64) "Homebrew" - "OpenJDK 11.0.15" /opt/homebrew/Cellar/openjdk@11/11.0.15/libexec/openjdk.jdk/Contents/Home
1.8.0_332 (arm64) "Azul Systems, Inc." - "Zulu 8.62.0.19" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
/opt/homebrew/Cellar/openjdk/18.0.1.1/libexec/openjdk.jdk/Contents/Home
Related Topics
Java Reverse an Int Value Without Using Array
What's the Best Way to Distribute Java Applications
In Java How to Sort One List Based on Another
Differencebetween Set and List
Casting Object Array to Integer Array Error
How to Escape Special HTML Characters in Jsp
JSON Java 8 Localdatetime Format in Spring Boot
Xml Configuration Versus Annotation Based Configuration
How to Re-Run Failed Junit Tests Immediately
Jfreechart Scaling of Boxplots with Several Categories
No @Xmlrootelement Generated by Jaxb
Spring MVC: How to Perform Validation
Java: Check the Date Format of Current String Is According to Required Format or Not
Parse JSON from Httpurlconnection Object