How to download, install & set $JAVA_HOME on macOS

This article shows you how to download, install and set the $JAVA_HOME on macOS.

Download the Java JDK, for example jdk-8u321-macosx-x64.dmg.

Steps to set the $JAVA_HOME environment variable on macOS.

  • Find out your macOS version.
  • Find out which shell you are using, bash or zsh?
  • For zsh shell, export $JAVA_HOME at  .zshenv  or  .zprofile  or  .zshrc 
  • For bash shell, export $JAVA_HOME at  .bash_profile or  .bashrc 
  • Source with source .zshenv
  • Test with echo $JAVA_HOME
  • Done.

Find out your macOS version

  • Click on the  icon in your Mac.
  • Click on 'About this Mac'.

Find out which cell you are using, bash or zsh?

On macOS 10.15 Catalina and later, the default terminal shell switch from the  bash  (Bourne-again shell) to  zsh  (Z shell).
  • For bash shell, we can put the environment variables at  .bash_profile or  .bashrc 
  • For zsh shell, we can put the environment variables at   .zshenv  or  .zprofile  or  .zshrc 
  • To know, your default Terminal shell open the Terminal on Mac.
  • Type  echo $SHELL 
  • Press return key.
    • For example,
    • echo $SHELL
    • /bin/zsh 

What is /usr/libexec/java_home

On Mac OS X 10.5 Leopard and later, we can use  /usr/libexec/java_home  to return the location of the default JDK.

/usr/libexec/java_home

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

Also, find all installed JDKs.

/usr/libexec/java_home -V

Matching Java Virtual Machines (2):

    17.0.1 (x86_64) "Oracle Corporation" - "Java SE 17.0.1" /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home

    1.8.0_321 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home(Default JDK)

Also, run a specified JDK command.

/usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home

Set $JAVA_HOME 

On macOS 10.15 Catalina and later, the  zsh  is the default Terminal shell, and we can set the $JAVA_HOME environment variables at   .zshenv  or  .zprofile  or  .zshrc 

Open the .zshenv

open .zshenv

Add the following content

export JAVA_HOME=$(/usr/libexec/java_home)

Source the file and print the $JAVA_HOME, done.

source .zshenv
echo $JAVA_HOME

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

Switch between different JDK versions

For example, this macOS contains two JDK: 1.8 and 17, and the default JDK is 17.

/usr/libexec/java_home -V

Matching Java Virtual Machines (2):

    17.0.1 (x86_64) "Oracle Corporation" - "Java SE 17.0.1" /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home

    1.8.0_321 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home(Default JDK)

For zsh shell, edit the .zshenv

open .zshenv

/usr/libexec/java_home -v "${Version}" to activate a specified JDK version.

Add the following content to activate the JDK 1.8

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Source the file and print the $JAVA_HOME, done.

source .zshenv
echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home






Comments

Popular posts from this blog

n - Interactively Manage Your Node.js Versions on macOS