How to set JAVA_HOME environment variable on Mac OS X 10.9?
Solution 1:
If you're using bash, all you have to do is:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
In either case, restart your shell.
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v
flag to java_home
like so:
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
Solution 2:
I did it by putting
export JAVA_HOME=`/usr/libexec/java_home`
(backtics) in my .bashrc. See my comment on Adrian's answer.
Solution 3:
I just spent 2 hours setting this variable. The other answers did not work properly for me. I'm using macOS Catalina 10.15.4.
First, find your actual Java SDK Home directory:
/usr/libexec/java_home
Manually navigate there to make sure you don't have any mistakes due to incorrect versions, etc. For me, this was:
/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Next, edit your terminal's profile. If you're using zsh, this will be:
vim ~/.zshrc
If you're not using zsh, this will be:
vim ~/.bash_profile
Inside, add the following new line anywhere in the file:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Restart your terminal app (or source ~/.bash_profile), and it should work properly.
Solution 4:
In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home
, just export $JAVA_HOME
in file ~/. bash_profile
or ~/.profile
.
Open the terminal and run the below command.
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
save and exit from vim editor, then run the source command on .bash_profile
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
Solution 5:
Set $JAVA_HOME environment variable on latest or older Mac OSX.
Download & Install install JDK
- First, install JDK
- Open terminal check java version
$ java -version
Set JAVA_HOME environment variable
- Open .zprofile file
$ open -t .zprofile
Or create . zprofile file
$
open -t .zprofile
- write in .zprofile
export JAVA_HOME=$(/usr/libexec/java_home)
Save .zprofile and close the bash file & then write in the terminal for work perfectly.
$ source .zprofile
Setup test in terminal
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home