Java is a popular programming language used to build web, mobile, and desktop applications. To create and run Java programs, you need the Java Development Kit (JDK). OpenJDK is the free and open-source version of the JDK, widely used by developers on macOS and other systems.
You can install OpenJDK on macOS in two simple ways:
- Using Homebrew: Easy and quick installation using the macOS package manager.
- Manual Installation: Download and install the JDK directly from the official website.
Methods to Install OpenJDK in macOS
Method 1: Using Homebrew
Homebrew is a package manager for macOS that makes software installation simple and quick.
Step 1: Check If Java Is Already Installed
- Before installing OpenJDK, check if Java is already installed on your macOS:
- Open the Terminal (press ⌘
Command + Space, typeTerminal, and hit Enter). - Type the following command:
java -version
- If Java is installed, you will see the version number. If not, you will see a message saying Java is not installed.
Step 2: Install Homebrew (If Not Installed)
- Homebrew is a package manager for macOS that makes it easy to install software like OpenJDK. To check if Homebrew is installed, run:
brew --version
- If Homebrew is not installed, install it using this command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Follow the on-screen instructions to complete the installation.
Step 3: Install OpenJDK
- Once Homebrew is installed, you can use it to install the latest version of OpenJDK:
- Open the Terminal run the following command:
brew install openjdk
- Homebrew will download and install the latest version of OpenJDK.
Step 4: Verify the Installation
- To confirm that OpenJDK has been installed successfully, check the version:
java -version
- You should see output showing the installed version of OpenJDK, such as:
openjdk version "21.0.1" 2025-01-01
OpenJDK Runtime Environment (build 21.0.1+12)
OpenJDK 64-Bit Server VM (build 21.0.1+12, mixed mode)
Step 5: Set JAVA_HOME (Optional)
- Some applications require the
JAVA_HOMEenvironment variable to be set. Here’s how you can set it: - Open the Terminal and edit your shell configuration file. If you are using zsh (default shell on macOS), run:
nano ~/.zshrc
- Add the following line to the file:
export JAVA_HOME="$(brew --prefix openjdk)/libexec/openjdk.jdk/Contents/Home"
- Save the file (press
Ctrl + O, thenEnter, andCtrl + Xto exit). - Apply the changes by running:
source ~/.zshrc
- Verify the
JAVA_HOMEvariable:
echo $JAVA_HOME
- This should display the path to your OpenJDK installation.
Method 2: Manual Installation
If you prefer not to use Homebrew, you can manually download and install OpenJDK from the official website:
Step 1: Download OpenJDK
- Visit the official OpenJDK website.
- Download the macOS
.tar.gzfile for your desired version.
Step 2: Extract the File
- Double-click the downloaded file to extract it.
Step 3: Move the Folder
- Open Terminal and run:
sudo mv jdk-21.jdk /Library/Java/JavaVirtualMachines/
Replace jdk-21.jdk with your actual folder name if different.
Step 4: Verify Installation
java -version
Step 5: Set JAVA_HOME
- Open your shell configuration file:
nano ~/.zshrc
- Add:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home"
- Apply the changes:
source ~/.zshrc
- Verify:
echo $JAVA_HOME