How to set JAVA_HOME in Mac permanently?
You can use /usr/libexec/java_home -v <version you want>
to get the path you need for JAVA_HOME
. For instance, to get the path to the 1.7 JDK you can run /usr/libexec/java_home -v 1.7
and it will return the path to the JDK. In your .profile
or .bash_profile
just add
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
and you should be good. Alternatively, try and convince the maintainers of java tools you use to make use of this method to get the version they need.
To open '.bash_profile' type the following in terminal :
nano ~/.bash_profile
and add the following line to the file:
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
Press CTRL+X to exit the bash. Press 'Y' to save changes.
To check whether the path has been added, type following in terminal:
source ~/.bash_profileecho $JAVA_HOME
I was facing the same issue in MAC Catalina, If I edit .bash_profile i found export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/HomeBut When I run terminal echo $JAVA_HOME it was returning empty, Later I found that the file .zshrc was missing I created this file with
touch .zshrc
Then edit it by nano .zshrc
and wrote
source ~/.bash_profile
Which solves my issue permanently
To set your Java path on mac:
- Open terminal on mac, change path to the root cd ~
- vi .bash_profile (This opens the bash_profile file)
Click I to insert text and use the following text to set JAVA_HOME and PATH
- export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home'
export PATH=$JAVA_HOME/bin:$PATH
- Type :wq to save and exit the file.
- Type source .bash_profile to execute the .bash_profile file.
- You can type echo $JAVA_HOME or echo $PATH