How to set JAVA_HOME in Mac permanently?
Solution 1
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_profile
echo $JAVA_HOME
Solution 2
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/Home But 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
Solution 3
Installing Java on macOS 11 Big Sur
:
- the easiest way is to select OpenJDK 11 (LTS), the HotSpot JVM, and macOS x64 is to get the latest release here: adoptopenjdk.net
- Select macOS and x64 and download the
JDK
(about 190 MB), which will put theOpenJDK11U-jdk_x64_mac_hotspot_11.0.9_11.pkg
file into your~/Downloads folder
- Clicking on pkg file, will install into this location:
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk
- Almost done. After opening a terminal, the successful installation of the JDK can be confirmed like so:
java --version
- output:
openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)
-
JAVA_HOME
is an important environment variable and it’s important to get it right. Here is a trick that allows me to keep the environment variable current, even after a Java Update was installed. In~/.zshrc
, I set the variable like so:export JAVA_HOME=$(/usr/libexec/java_home)
- In previous macOS versions, this was done in
~/.bash_profile
. Anyway, open a new terminal and verify:echo $JAVA_HOME
- output:
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
- output:
TEST: Compile and Run your Java Program
- Open a text editor, copy the code from below and save the file as
HelloStackoverflow.java
.
public class HelloStackoverflow {
public static void main(String[] args){
System.out.println("Hello Stackoverflow !");
}//End of main
}//End of HelloStackoverflow Class
- From a terminal set the working directory to the directory containing
HelloStackoverflow.java
, then type the command:
javac HelloStackoverflow.java
-
If you're lucky, nothing will happen
-
Actually, a lot happened.
javac
is the name of the Java compiler. It translates Java intoJava Bytecode
, an assembly language for the Java Virtual Machine (JVM). The Java Bytecode is stored in a file calledHelloStackoverflow.class
. -
Running: type the command:
java HelloStackoverflow
# output:
# Hello Stackoverflow !
Solution 4
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
Solution 5
Besides the settings for bash/ zsh terminal which are well covered by the other answers, if you want a permanent system environment variable for terminal + GUI applications (works for macOS Sierra; should work for El Capitan too):
launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 1.8)
(this will set JAVA_HOME to the latest 1.8 JDK, chances are you have gone through serveral updates e.g. javac 1.8.0_101, javac 1.8.0_131)
Of course, change 1.8 to 1.7 or 1.6 (really?) to suit your need and your system
Vishal Tavande
Updated on November 13, 2021Comments
-
Vishal Tavande over 2 years
I am trying to set
JAVA_HOME
by enteringexport JAVA_HOME=/Library/Java/Home
at terminal. It sets theJAVA_HOME
for current session.How can I set it permanently?
-
Jonas Rabbe over 9 yearsIf this answered your question, Vishal, could you please mark it as the answer? Thanks.
-
Roy Hinkley over 8 yearsIt's not the answer because it does not permanently set the
JAVA_HOME
environment variable I suppose, which is what the OP asked for. This is onlytemporary for the current session.
A reboot will change it back to the permanently set variable. -
Elist almost 7 yearsThis is the correct answer for GUI Applications (not the OP's question, though...). Work like charm on macOS Sierra.
-
cabaji99 over 6 yearsThis command dont work on terminal.Anyone? tried the bash_profile and nothing.
-
Merlin about 6 yearsRe Android Addict comment: There are no 'permanent' environment variables for shells, that's why you have a .bash_profile, .bashrc, .zshrc, etc. This answer will solve the question and should be chosen.
-
MithunS about 5 yearsI tried this, it changes for the shell session, but has no effect on a new shell session.
-
Arv about 5 yearsMac OS automatically executes .bash_profile on every terminal session.Are you still facing the issue ? Did you try restarting ?
-
tryman about 5 years(This requires vim too though, doesn't it? Not that bad of a problem, but the user has to install it.)
-
user11248574 about 5 yearsvim is pre installed in mac
-
Boris Y. over 4 yearsThank you. I have tried 5 different things, but your solution actually helped me. Using macOS 10.14.5 (18F132) and setting JDK version.
-
Kiren James almost 4 yearsWorked for MAC Catalina solve..! Thanks for sharing the tip!
-
Sumit Ramteke almost 4 yearsone either use vi or even textmate. which sh'd not be an issue
-
Bostone over 3 yearsI had problems with this approach when having multiple versions of Java installed especially when one of these installed with
brew
-
paradocslover over 3 yearsIn my case, this worked for that instance only. Though replacing
bash_profile
withzshrc
worked for me -
ktappe over 3 years~/.bash_profile is not a default file; most won't have it, so you can't just start editing it. And you can't just create it because it has syntax; it needs to be a full script file.
-
ktappe over 3 yearsTHIS is the answer, not all the other stuff above. (Though note that Dilip left off the trailing " at the end of line 3.)
-
Ritesh Adulkar over 3 yearsWowww...That's Awesome. Thanks for sharing.
-
learner over 3 yearsI am using jenv and figured out that adding JAVA_HOME after jenv configuration in zshrc file sets the JAVA_HOME variable else I was getting an empty value.
export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_252)
-
bellotas over 3 yearsThis is genius!
-
mr.sanatbek over 3 yearsThank you, please change format answer to the following format. Of course it our responsibility to change version. But anyway I have merged your answer with @Arv answer below. THen It worked. JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home'
-
Kerem over 3 yearssince yesterday i am trying to write it to the .bash_profile file and wondering why it is not working. .zshrc file !!! You saved my day. Thank you
-
Ajay Kr Choudhary about 3 years@Kerem It depends what is your default shell Set. You can know this by doing
echo $SHELL
If it is/bin/bash
then you need to change in .bash_profile, if it is/bin/zsh
then you should set it in .zshrc file. -
Ananth Kamath over 2 yearsThis should be marked as the right answer, it worked for me as well.
-
Onat Korucu over 2 yearsIn my case, the first command worked even without "-v <version>". Why is "-v <version>" beneficial? Can you have a separate JAVA_HOME for each version?
-
Alexei Artsimovich over 2 years@paradocslover renaming bash_profile to zshrc (I have created bash_profile in the first place) saved my life. Thanks!
-
Dmitri Algazin over 2 yearsgetting all versions use capital V, like: "/usr/libexec/java_home -V"
-
Eren Jaeger about 2 yearsthis worked on Monterey!
-
lbrobinho almost 2 yearsI think only Oh_my_zsh users need to modify zshrc file.
-
Admin almost 2 yearsopen new terminal and hit this cmd for open bash file xxxxx@XXX-MacBook-Pro ~ % cd ~/ xxxx@XXX-MacBook-Pro ~ % touch ~/ xxxx@XXX-MacBook-Pro ~ % open -e .bash_profile