How to install OpenJDK on macOS?

Tue, 11 Jul 2023 12:43:28 GMT

Steps:

1. Install OpenJDK using brew

brew install openjdk


brew install openjdk

2. Configure environment variable JAVA_HOME.

Configure manually:

Add the below line to ~/.zshrc:

export JAVA_HOME="/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home"


export JAVA_HOME="/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home"

Restart your terminal.

Or you can run the below command:

echo -e "\nexport JAVA_HOME=\"$(brew --prefix openjdk)/libexec/openjdk.jdk/Contents/Home\"" >> ${ZDOTDIR:-~}/.zshrc && exec zsh


echo -e "\nexport JAVA_HOME=\"$(brew --prefix openjdk)/libexec/openjdk.jdk/Contents/Home\"" >> ${ZDOTDIR:-~}/.zshrc && exec zsh