[java] How to set JAVA_HOME in Mac permanently?

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?

This question is related to java macos java-home

The answer is


If you are using the latest versions of macOS, then you cannot use ~/.bash_profile to export your environment variable since the bash shell is deprecated in the latest version of macOS.

  • Run /usr/libexec/java_home in your terminal and you will get things like /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
  • Add export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home to .zshrc

1) The first step is to if you have Java installed and running your system.

which java

Usually, it should be /usr/bin/java.

2) JAVA_HOME is essentially the full path of the directory that contains a sub-directory named bin which in turn contains the java.

cd /Library/Java/
  • You should have bin folder inside /Java folder and in turn it must contain the [java] executable package.

Folder structure for Mac OS

3.1) If you want to set the path only for the current session then execute this command in your terminal export JAVA_HOME=/Library/Java

3.2) If you want it to persist, you will have to add the command to your ~/.bash_profile file

  • Open up Terminal vi ~/.bash_profile
  • Add these lines to terminal export JAVA_HOME=/Library/Java/Home and save it
  • Execute below command source ~/.bash_profile

4) Verify you have correctly added JAVA_HOME path. Below command should give you the proper Java version.

That's it and your ready to use!!


First, figure out where your java home is by running the command /usr/libexec/java_home -v <version> replacing with whatever version of OpenJDK your running.

Next use vim ~/.bash_profile to edit your bash profile. Add export JAVA_HOME="<java path>" replacing with the path to your java home found in the last step.

Finally, run the command source ~/.bash_profile

This should permanently set your JAVA_HOME environment variable.

To make sure it worked run echo $JAVA_HOME and make sure it returns the path you set


to set JAVA_HOME permenantly in mac make sure you have JDK installed in your system, if jdk is not installed you can download it from here https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

After installing jdk follow these steps :-

1) Open Terminal

2) Type "vim .bash_profile"

3) press "i" to edit or enter the path

4) Type your java instalation dir :- export JAVA_HOME=$(/usr/libexec/java_home)

5) Click ESC then type ":wq" (save and quit in vim)

6) Then type "source .bash_profile"

7) type "echo $JAVA_HOME" if you see the path you are all set.

THANK YOU


Declare two export inside your .bashrc or .zshrc:

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) 
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

Add alias for quick change:

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'

set default to Java 11

java11

export PATH

export PATH=$JAVA_HOME/bin:$PATH

you could change java11 by java8 inside your .bashrc/zshrc file to change permanently your java version


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


Adding to Dilips's answer, if you are working with JDK 9, use the following (my JDK version is 9.0.4) in Step # 3:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/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/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


This link may helps you: https://developer.apple.com/library/archive/qa/qa1067/_index.html

Also, you can put the environment variable in one of these files:

~/.bashrc
~/.bash_profile
~/.profile

run this command on your terminal(here -v11 is for version 11(java11))-:

/usr/libexec/java_home -v11

you will get the path on your terminal something like this -:

/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

now you need to open your bash profile in any editor for eg VS Code

if you want to edit your bash_profile in vs code then run this command -:

code ~/.bash_profile

else run this command and then press i to insert the path. -:

open ~/.bash_profile

you will get your .bash_profile now you need to add the path so add this in .bash_profile (path which you get from 1st command)-:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

if you were using code editor then now go to terminal and run this command to save the changes -:

source ~/.bash_profile

else press esc then :wq to exit from bash_profile then go to terminal and run the command given above. process completed. now you can check using this command -:

echo $JAVA_HOME

you will get/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home


To set JAVA_HOME permanently in Mac, I tried following steps.

  1. Download and install Java JDK to your Mac. When you install a Java JDK version which will be installed in the following location by default in MAC.

/Library/Java/JavaVirtualMachines

  1. Open the .bash_profile file (Here My Mac version is MacOS High Sierra. You may need to open .zshrc file in some different MacOS versions).

atom ~/.bash_profile

  1. Add following to your bash_profile file.

Change the JDK version accordingly

export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home'
export PATH=$JAVA_HOME/bin:$PATH
  1. Open the Terminal and execute following.

source ~/.bash_profile

Open a new terminal and check 'echo $JAVA_HOME'

Thanks.


add following

setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home

in your ~/.login file:


If you are using fish shell. Then all the variables can be set in .config/fish/config.fish

vim .config/fish/config.fish

Add the following lines

set -g JAVA_HOME "your_path_to_jdk"

save and exit out of vim.

This should be setting your JAVA_HOME. Thanks


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

sql-surfer and MikroDel,

actually, the answer is not that complicated! You just need to add:

export JAVA_HOME=(/usr/libexec/java_home)

to your shell profile/configuration file. The only question is - which shell are you using? If you're using for example FISH, then adding that line to .profile or .bash_profile will not work at all. Adding it to config.fish file though will do the trick. Permanently.


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


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

Try this link http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/

This explains correctly, I did the following to make it work

  1. Open Terminal
  2. Type vim .bash_profile
  3. Type your java instalation dir in my case export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
  4. Click ESC then type :wq (save and quit in vim)
  5. Then type source .bash_profile
  6. echo $JAVA_HOME if you see the path you are all set.

Hope it helps.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to java-home

Set ANDROID_HOME environment variable in mac Error: JAVA_HOME is not defined correctly executing maven How to set JAVA_HOME in Linux for all users Android Studio error: "Environment variable does not point to a valid JVM installation" Installing Android Studio, does not point to a valid JVM installation error How can I change Mac OS's default Java VM returned from /usr/libexec/java_home How to set JAVA_HOME path on Ubuntu? Where is the Java SDK folder in my computer? Ubuntu 12.04 How to set JAVA_HOME in Mac permanently? How to set CATALINA_HOME variable in windows 7?