How to set JAVA_HOME in Mac permanently?

177,675

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 the OpenJDK11U-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 enter image description here
  • 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

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 into Java Bytecode, an assembly language for the Java Virtual Machine (JVM). The Java Bytecode is stored in a file called HelloStackoverflow.class.

  • Running: type the command:

java HelloStackoverflow

# output:
# Hello Stackoverflow !

enter image description here

Solution 4

To set your Java path on mac:

  1. Open terminal on mac, change path to the root cd ~
  2. vi .bash_profile (This opens the bash_profile file)
  3. 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

      1. Type :wq to save and exit the file.
      2. Type source .bash_profile to execute the .bash_profile file.
      3. 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

Share:
177,675
Vishal Tavande
Author by

Vishal Tavande

Updated on November 13, 2021

Comments

  • Vishal Tavande
    Vishal Tavande over 2 years

    I am trying to set JAVA_HOME by entering export JAVA_HOME=/Library/Java/Home at terminal. It sets the JAVA_HOME for current session.

    How can I set it permanently?

  • Jonas Rabbe
    Jonas Rabbe over 9 years
    If this answered your question, Vishal, could you please mark it as the answer? Thanks.
  • Roy Hinkley
    Roy Hinkley over 8 years
    It'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 only temporary for the current session. A reboot will change it back to the permanently set variable.
  • Elist
    Elist almost 7 years
    This is the correct answer for GUI Applications (not the OP's question, though...). Work like charm on macOS Sierra.
  • cabaji99
    cabaji99 over 6 years
    This command dont work on terminal.Anyone? tried the bash_profile and nothing.
  • Merlin
    Merlin about 6 years
    Re 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
    MithunS about 5 years
    I tried this, it changes for the shell session, but has no effect on a new shell session.
  • Arv
    Arv about 5 years
    Mac OS automatically executes .bash_profile on every terminal session.Are you still facing the issue ? Did you try restarting ?
  • tryman
    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
    user11248574 about 5 years
    vim is pre installed in mac
  • Boris Y.
    Boris Y. over 4 years
    Thank 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
    Kiren James almost 4 years
    Worked for MAC Catalina solve..! Thanks for sharing the tip!
  • Sumit Ramteke
    Sumit Ramteke almost 4 years
    one either use vi or even textmate. which sh'd not be an issue
  • Bostone
    Bostone over 3 years
    I had problems with this approach when having multiple versions of Java installed especially when one of these installed with brew
  • paradocslover
    paradocslover over 3 years
    In my case, this worked for that instance only. Though replacing bash_profile with zshrc worked for me
  • ktappe
    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
    ktappe over 3 years
    THIS 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
    Ritesh Adulkar over 3 years
    Wowww...That's Awesome. Thanks for sharing.
  • learner
    learner over 3 years
    I 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
    bellotas over 3 years
    This is genius!
  • mr.sanatbek
    mr.sanatbek over 3 years
    Thank 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.jd‌​k/Contents/Home'
  • Kerem
    Kerem over 3 years
    since 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
    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
    Ananth Kamath over 2 years
    This should be marked as the right answer, it worked for me as well.
  • Onat Korucu
    Onat Korucu over 2 years
    In 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
    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
    Dmitri Algazin over 2 years
    getting all versions use capital V, like: "/usr/libexec/java_home -V"
  • Eren Jaeger
    Eren Jaeger about 2 years
    this worked on Monterey!
  • lbrobinho
    lbrobinho almost 2 years
    I think only Oh_my_zsh users need to modify zshrc file.
  • Admin
    Admin almost 2 years
    open 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