Java, a robust and versatile programming language, is a fundamental tool for developers working across various platforms. On macOS, installing and configuring Java is a straightforward process, essential for running applications, developing software, and interacting with complex systems. This guide will walk you through the essential steps to ensure Java is properly set up on your Mac, whether you’re a seasoned developer or just beginning your journey into the world of programming. Understanding the different Java Development Kits (JDKs) and their implications is crucial for a smooth and efficient development experience.
Understanding Java Development Kits (JDKs) on macOS
Before diving into the installation process, it’s important to grasp what a JDK is and why there are different versions. A JDK is a bundle of software development tools that includes the Java Runtime Environment (JRE), compilers, debuggers, and other utilities necessary for creating and running Java applications. For macOS, you’ll primarily encounter two types of installations: the Oracle JDK and OpenJDK.
Oracle JDK vs. OpenJDK
Oracle JDK: This is the official, proprietary implementation of Java provided by Oracle. It’s widely used and generally offers excellent performance and comprehensive features. Oracle JDK has different licensing terms depending on the version and your intended use. For commercial use of certain versions, a license may be required.
OpenJDK: This is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It serves as the basis for most other Java distributions, including Oracle JDK. OpenJDK is maintained by a community of developers and is often favored for its open nature and flexibility. Various distributions of OpenJDK are available, such as Adoptium Temurin (formerly AdoptOpenJDK), Amazon Corretto, and Microsoft Build of OpenJDK.
Java SE Versions and Compatibility
Java has evolved significantly over the years, with new versions introducing new features, performance improvements, and security enhancements. When installing Java on your Mac, you’ll need to consider which Java SE version your applications or development projects require.
- LTS (Long-Term Support) Versions: These versions, like Java 8, 11, 17, and soon 21, receive extended support and updates, making them ideal for production environments where stability and long-term maintenance are paramount.
- Non-LTS Versions: These are released more frequently and offer the latest features but have shorter support lifecycles. They are excellent for exploring new Java capabilities or for projects that can accommodate more frequent upgrades.
For most users, installing the latest LTS version or a specific version required by their development tools is the recommended approach.
Installing Java Development Kit (JDK) on Mac
The process of installing a JDK on macOS typically involves downloading the installer and running it. For ease of management, especially when working with multiple Java versions, using a version manager like jenv or Homebrew is highly recommended.
Method 1: Manual Installation (Oracle JDK)
This method involves downloading the installer directly from the Oracle website.
- Visit the Oracle Java Downloads Page: Navigate to the official Oracle Java SE Downloads page. You can find this by searching for “Oracle Java SE Downloads” or by going to
oracle.com/java/technologies/downloads/. - Select the Desired Java Version: Choose the Java SE version you wish to install. LTS versions are generally recommended for stability.
- Download the macOS Installer: Under the “macOS” tab, you will find installer options. Typically, you’ll want the “x64 DMG Installer.” Click to download the
.dmgfile. - Run the Installer:
- Locate the downloaded
.dmgfile in your Downloads folder. - Double-click the file to mount the disk image.
- A window will appear with a
.pkginstaller. Double-click the.pkgfile to start the installation wizard. - Follow the on-screen prompts. You will likely need to agree to the license agreement and may be prompted for your administrator password.
- Locate the downloaded
- Verify the Installation:
- Open the Terminal application. You can find it in
Applications > Utilitiesor by searching with Spotlight (Cmd + Space). - Type the following command and press Enter:
bash
java -version
- If the installation was successful, you should see output indicating the installed Java version. For example:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment Temurin-17.0.8+7 (build 17.0.8+7)
OpenJDK 64-Bit Server VM Temurin-17.0.8+7 (build 17.0.8+7, mixed mode, sharing)
- To verify the JDK specifically, you can run:
bash
javac -version
This command checks for the Java compiler, which is part of the JDK.
- Open the Terminal application. You can find it in
Method 2: Using Homebrew (Recommended for OpenJDK and ease of management)
Homebrew is a popular package manager for macOS that simplifies the installation and management of software. It’s an excellent way to install OpenJDK distributions.
- Install Homebrew (if you don’t have it):
- Open Terminal.
- Paste the following command and press Enter:
bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Follow the on-screen instructions to complete the installation.
- Update Homebrew:
- Run the following command to ensure Homebrew has the latest package information:
bash
brew update
- Run the following command to ensure Homebrew has the latest package information:
- Search for Java Distributions:
- To see available OpenJDK distributions, use:
bash
brew search jdk
- You’ll likely see options like
openjdk,temurin,zulu, etc.temurin(from Adoptium) is a very popular and well-supported choice.
- To see available OpenJDK distributions, use:
- Install a JDK:
- To install Adoptium Temurin JDK 17, for example:
bash
brew install temurin
- To install a specific version, you might need to specify it, e.g.,
brew install temurin@11.
- To install Adoptium Temurin JDK 17, for example:
- Link the JDK: Homebrew might install it, but you might need to link it to be the default. The output of
brew installusually provides instructions. For example, to make Temurin the default:
bash
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
(Note: The path might vary slightly depending on the Homebrew prefix or the specific JDK installed. Always check the output ofbrew installfor exact instructions.) - Verify the Installation: Open Terminal and run:
bash
java -version
java -version
You should see the version of the JDK you installed via Homebrew.

Method 3: Using jenv (Advanced Java Version Management)
For developers who frequently switch between different Java versions for various projects, jenv is an invaluable tool. It allows you to easily set global, local (per-project), and shell-specific Java versions.
- Install
jenvvia Homebrew:
bash
brew install jenv
- Configure your shell for
jenv: Add the following lines to your shell configuration file (~/.zshrcfor Zsh,~/.bash_profileor~/.bashrcfor Bash):
bash
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
After adding these lines, either restart your terminal or runsource ~/.zshrc(or your respective shell file) to apply the changes. - Install JDKs (as described in Method 1 or 2): Make sure you have the desired JDKs installed on your system.
- Add Installed JDKs to
jenv:- Run the following command to list the JDKs
jenvcan find:
bash
jenv doctor
- If your installed JDKs are not automatically detected, you might need to add them manually. For example, to add a JDK installed via Homebrew at
/usr/local/opt/openjdk:
bash
jenv add /usr/local/opt/openjdk
- Repeat this for all JDKs you wish to manage with
jenv.
- Run the following command to list the JDKs
- Set Java Versions:
- Global Default:
bash
jenv global 17
(Replace17with the desired version number). - Local (Per Project): Navigate to your project directory in Terminal and run:
bash
jenv local 11
This creates a.java-versionfile in your project directory, ensuring this Java version is used whenever you’re in that directory. - Shell Specific:
bash
jenv shell 8
This sets the Java version only for your current terminal session.
- Global Default:
- Verify the Current Version:
bash
java -version
jenvwill output the currently active Java version based on your global, local, or shell settings.
Configuring Environment Variables
While modern JDK installations and tools like Homebrew and jenv often manage environment variables automatically, it’s sometimes necessary to understand or manually configure them, especially if you encounter issues or need to point to specific Java installations.
The most common environment variables related to Java are:
JAVA_HOME: This variable points to the root directory of your Java installation. Many Java-based tools and applications rely onJAVA_HOMEto locate the JDK.PATH: This variable tells your operating system where to find executable programs. When you typejavaorjavacin the terminal, your system searches directories listed inPATH.
Setting JAVA_HOME
If your JAVA_HOME is not set correctly, you might see errors like “JAVA_HOME is not set and no default is specified.”
- Find your Java Installation Path:
- If you installed manually, the JDK is usually in
/Library/Java/JavaVirtualMachines/. You’ll need to find the specific version folder, e.g.,/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home. - If you used Homebrew, the path is often
/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Homeor similar. - You can also try finding it with:
bash
/usr/libexec/java_home
This command is a macOS utility that can help locate the Java installation path.
- If you installed manually, the JDK is usually in
- Edit your Shell Profile:
- Open your shell’s configuration file in a text editor (e.g.,
nano ~/.zshrcornano ~/.bash_profile). - Add the following lines, replacing
/path/to/your/jdkwith the actual path you found:
bash
export JAVA_HOME=/path/to/your/jdk
export PATH=$JAVA_HOME/bin:$PATH
- Save the file and exit the editor.
- Open your shell’s configuration file in a text editor (e.g.,
- Apply Changes:
- In the Terminal, run
source ~/.zshrc(or your respective shell file) or simply close and reopen your Terminal window.
- In the Terminal, run
- Verify:
bash
echo $JAVA_HOME
java -version
Note: If you are using jenv, it manages JAVA_HOME for you, so manual setting is usually not required.
Common Issues and Troubleshooting
“Java command not found”
This error typically means Java is not installed, or its bin directory is not in your PATH.
- Check Installation: Run
java -version. If it fails, revisit the installation steps. - Check PATH: Ensure
$JAVA_HOME/binis included in yourPATHvariable. - Restart Terminal: Sometimes, a simple restart of the Terminal application is necessary for changes to take effect.
Incorrect Java Version
If you have multiple Java versions installed and the wrong one is being used:
jenv: Usejenv global,jenv local, orjenv shellto set the desired version.- Manual
PATH: Verify the order of directories in yourPATH. The first occurrence of abindirectory containingjavawill be used. Ensure your desiredJAVA_HOME/binappears earlier than others if necessary. - Homebrew: Ensure you have linked the correct JDK or that your
PATHpoints to the Homebrew-managed Java.
Permissions Issues
During installation, you might encounter permission errors. Ensure you are running installers with administrator privileges when prompted. If manually editing files, use sudo if necessary.
![]()
Conclusion
Installing Java on a Mac is a fundamental step for anyone engaging with Java development or running Java-based applications. By understanding the differences between JDKs, choosing the right installation method (manual, Homebrew, or jenv), and correctly configuring your environment variables, you can ensure a seamless and productive experience. For most users, Homebrew offers a convenient and robust way to manage Java installations, while jenv provides advanced control for complex development environments. With Java properly installed and configured, you are well-equipped to harness its power on your macOS system.
