How to install Java on macOS

Written by:

April 17, 2024

0 mins read

What is Java?

Java is a high-level, class-based, object-oriented programming language that was designed to have as few implementation dependencies as possible. It is a computing platform that was first introduced by Sun Microsystems in 1995. Many applications and websites today won't work unless you have Java installed, and more are created every day.

Java is well-known for its "Write Once, Run Anywhere" functionality. This means that compiled Java code can run on all platforms supporting Java, without the need for recompilation. It's fast, secure, and reliable, making it suitable for everything from mobile applications to enterprise-level solutions.

public class HelloWorld {
    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");

In the code snippet above, we can see one of the simplest Java programs possible, a "Hello, World" program. This small program demonstrates several of Java's key features — class definition, method definition, and the well-known `main` method, which is the entry point of any Java application.

Why install Java on macOS?

Java is the foundation for developing and deploying enterprise applications and web-based content. It provides the base for community-driven libraries, frameworks, and APIs, making it an excellent language for developing robust and scalable applications.

For macOS users, installing Java on your system can open up a world of possibilities for software development. It's a fast, secure, and reliable platform that can be used to develop and deploy a wide range of applications, from simple web pages to complex enterprise solutions.

In the following sections, we will guide you through the process of installing Java on macOS, making sure you're ready to start developing applications in no time.

What is OpenJDK?

OpenJDK stands for Open Java Development Kit, a free open source implementation of the Java Platform, Standard Edition (Java SE). It's maintained by Oracle, and other major contributors include companies like IBM, Red Hat, and Apple. OpenJDK provides the official Reference Implementation of the Java SE Specification.

OpenJDK vs Oracle's JDK

OpenJDK and Oracle's JDK were originally quite similar, with the main difference being that Oracle's JDK included a few additional commercial features. However, since the release of Java 11, Oracle has made significant changes to its release model and licensing terms.

Oracle's JDK is now released under a commercial license for those using it in a production environment. In contrast, OpenJDK was released under the GPL open source license with a linking exception. This means that you can use OpenJDK without worrying about paying a license fee.

Moreover, Oracle's JDK has a feature release every six months, whereas OpenJDK has a new major release every three years with long-term support (LTS).

// code runs the same on both OpenJDK and Oracle's JDK
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");

In the above Java code, it doesn't matter whether you are using OpenJDK or Oracle's JDK. The output will be the same: `Hello, World!`.

The importance of OpenJDK in the Java ecosystem

OpenJDK plays a critical role in the Java ecosystem for a variety of reasons. First, it is the reference implementation of the Java SE platform, meaning it serves as the standard by which all other implementations are measured.

Second, its open source nature means that the wider developer community can contribute to its development. This not only leads to a more robust and secure product but also fosters an environment of collaboration and innovation.

Finally, OpenJDK's GPL license enables it to be freely used, distributed, and modified, which is a tremendous advantage for individual developers and organizations alike.

In conclusion, OpenJDK is an essential tool for any developer working with Java. Whether you're developing enterprise-level applications or just getting started with Java, OpenJDK offers a robust, secure, and open source platform that is the gold standard in the Java ecosystem.

Java pre-installation checks

Before installing Java on your macOS, it is essential to perform some pre-installation checks. These steps are crucial to avoid any conflicts with existing installations, ensure the system's compatibility, and, most importantly, confirm the need for the installation. This section will guide you through the process of:

  • Checking if Java is already installed on your macOS

  • Understanding which version of Java is currently installed, if any

Checking if Java is already installed on macOS

The first step is to check if Java is already installed on your system. macOS comes with Java installed by default, but it might not be the version you need for your specific use case.

To check if Java is installed, open your Terminal application (You can use Spotlight search and type `Terminal`), and type the following command:

java -version

If Java is installed, you should see an output similar to the following:

java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

The "1.8.0_271" indicates the version of Java that's installed.

If Java is not installed, you will receive a message stating that the `java` command could not be found.

Understanding which version of Java is currently installed

If Java is installed on your system, you need to know the exact version because different applications may require different versions of Java. The command `java -version` also helps us to understand the installed version.

The output of the `java -version` command provides the version number immediately after the word "version" in the first line. In the example above, the version number is "1.8.0_271", which represents Java 8 update 271. Understanding the version of Java installed on your system can help you decide whether to install a new version or utilize the current one.

Performing these pre-installation checks is crucial for a successful Java installation process. In the next sections, we will discuss the actual installation process of Java on macOS.

How to install OpenJDK on macOS with Homebrew

1. Checking the current Java version

Before starting the installation process, you should first check if Java is already installed on your macOS. To do this, open the Terminal application and run the following command:

java -version

If Java is already installed on your system, this command will display the current version. If not, Terminal will prompt you to install a JDK.

2. Installing OpenJDK with Homebrew

Homebrew is a free and open source software package management system that simplifies the installation of software on macOS. It's often termed as the "missing package manager for macOS." Homebrew's simplicity makes it the first choice for developers to handle software packages on macOS.

The next step is to download OpenJDK. You can download it directly from the OpenJDK website or use a package manager like Homebrew. If you don't have Homebrew installed, you can install it using the following command:

/bin/bash -c "$(curl -fsSL"

To install OpenJDK using Homebrew, run the following command in Terminal:

brew install openjdk@11

You can replace '11' with the version number of OpenJDK you want to install.

At the time of writing this article, the following OpenJDK Java releases are available on Homebrew:

  • openjdk@8

  • openjdk@9

  • openjdk@11

  • openjdk@17

3. Setting Java in the path environment variable

You should see a message displaying the version of the installed Java, which should match the version you installed. However, maybe you were greeted with the following message instead:

The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit for information on installing Java.

If you didn't see an informational message about the Java version, then you might need to add the Java home directory to your path such as this:

echo 'export PATH="/opt/homebrew/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

After which a successful Java version should show the following for an installation of Java 17 as is installed on my development environment:

java -version
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment Homebrew (build 17.0.9+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.9+0, mixed mode, sharing)

4. Verifying the installation

Finally, verify that OpenJDK is installed correctly by checking the Java version again:

java -version

If OpenJDK is installed correctly, this command should display the version of OpenJDK you installed.

And there you have it! You've successfully installed OpenJDK on macOS. This will allow you to run and develop Java applications on your Mac with ease.

5. Secure Java applications with Snyk

In the world of software development, security has become a non-negotiable feature. With cyber threats lurking around every corner, it is crucial to ensure the security of your Java applications. Here are some effective ways to achieve this security through Snyk, a leading platform for developer-first open source security.

Snyk Open Source helps you proactively find and fix vulnerabilities in your open source dependencies. It's an excellent tool to integrate into your Java development workflow because it's designed to fit right in without disrupting your work.

With Snyk Open Source, you can easily find, prioritize, and fix security vulnerabilities in your Java dependencies. Here is a simple command to test your project:

snyk test

Secure your Java applications with Snyk Open Source

How to install OpenJDK manually on macOS (using AdoptOpenJDK)

In this section, we will discuss how to download and install OpenJDK on your macOS manually. Though there are package managers like Homebrew that can automate this process, manual installation gives you more control over the version and configuration of the JDK.

Download OpenJDK

You can download the OpenJDK binaries from the official AdoptOpenJDK website. On the homepage, you'll find options to choose your desired version of OpenJDK and the operating system. For this tutorial, select the OpenJDK version you prefer (for example, OpenJDK 11) and choose macOS as your operating system. After selection, click on the `Latest release` button to download the JDK.

Install OpenJDK

Once the download is complete, the installer file (.pkg) will be in your Downloads folder. Double-click on the .pkg file to start the installation process. Follow the prompts on the installer wizard to install OpenJDK.

Setting the JAVA_HOME environment variable

Next, we need to set `JAVA_HOME` environment variable on your macOS. This variable points to the directory where JDK is installed and is necessary for running and developing Java applications.

Open Terminal and add the following lines to the `.bash_profile` or `.zshrc` file:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

In the above lines of code, `$(/usr/libexec/java_home)` dynamically finds the path of the current JDK installation. Save the file and exit.

To ensure the changes take effect, either restart the Terminal or run the following command:

source ~/.bash_profile
# or
source ~/.zshrc

Verifying the installation

To verify the installation and the environment variable setting, you can use the Terminal to display the version of the installed JDK. Run the following command:

1java -version

This should display information about the installed Java version, similar to this:

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.2+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.2+9, mixed mode)

If you see the OpenJDK version you installed in the output, congratulations! You have successfully manually installed OpenJDK on your macOS and set the JAVA_HOME environment variable correctly.

Switching between different Java versions

Once you have multiple versions of Java installed on your macOS, you might need to switch between them depending on the requirements of your project.

Switching Java versions with Homebrew

To switch between different Java versions, Homebrew uses a command called `jenv`. If you don't have `jenv` installed, you can install it using the following command:

brew install jenv

After you've installed `jenv`, you need to add the installed Java versions to `jenv`. For each version, navigate to the directory where it's installed and run the following command:

jenv add /Library/Java/JavaVirtualMachines/<java-version>.jdk/Contents/Home/

Replace `<java-version>` with the version of Java you want to add.

Once you've added the versions of Java you want to switch between, you can list them with the following command:

jenv versions

To switch to a specific Java version, use the `jenv global` command followed by the version number:

jenv global <java-version>

Again, replace `<java-version>` with the version of Java you want to switch to.

Remember, it's crucial to use the correct Java version for your applications to ensure compatibility and optimal performance.

Secure Java containers with Snyk Container

You might be running different Java versions in production across container workloads such as Kubernetes or through PaaS. Snyk Container provides a developer-first solution for finding and fixing vulnerabilities in container images and Kubernetes applications. If you're deploying your Java application in a container, Snyk Container is a must-have tool for you.

To test a Docker image for vulnerabilities, you can use the `snyk container test` command:

snyk container test <image-name>

This command will scan your Docker image for known vulnerabilities and provide a comprehensive report.

Secure your Java containers with Snyk Container

What is Maven?

Maven is a powerful project management tool that developers use for managing project builds, reporting, and documentation. As a comprehensive tool, Maven simplifies and standardizes the project build process. It handles compilation, distribution, documentation, team collaboration, and other tasks seamlessly. Maven increases reusability and takes care of most of the build-related tasks.

How to install Maven with Brew?

Homebrew is a package manager for macOS that simplifies the installation of software on Apple's macOS operating system. To install Maven with Brew, you need to have Homebrew installed on your machine. Follow the steps below:

1. Check if Homebrew is installed: Open Terminal and type the following command:

brew -v

If Homebrew is installed, the version will be displayed. If not, you need to install Homebrew first.

2. Install Homebrew: If Homebrew is not installed, you can install it by running the following command in Terminal:

/bin/bash -c "$(curl -fsSL"

3. Install Maven: Once Homebrew is installed, you can install Maven by running the following command:

brew install maven

4. Check Maven version: After the installation is complete, you can verify the installation by checking the Maven version:

mvn -v

This command should return the installed version of Maven and show an output similar to the following:

Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)
Maven home: /opt/homebrew/Cellar/maven/3.9.5/libexec
Java version: 21.0.1, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: en_IL, platform encoding: UTF-8
OS name: "mac os x", version: "14.1", arch: "aarch64", family: "mac"

How to install Maven from Apache source?

If you prefer not to use Homebrew or if you encounter issues with it, you can also install Maven directly from the Apache source. Here's how:

1. Download Maven: Download the latest Maven release from the Apache website). Choose the binary tar.gz file.

2. Extract the file: Once the tar.gz file is downloaded, extract it to your desired directory. You can use the following command to extract:

tar xzvf apache-maven-3.x.y-bin.tar.gz

3. Set up the environment variables: Add the bin directory of the created directory apache-maven-3.x.y to the PATH environment variable:

export PATH=/path/to/apache-maven-3.x.y/bin:$PATH

4. Verify the installation: Confirm that Maven is installed correctly by running the same version command as before:

mvn -v

This command should return the installed version of Maven.

In conclusion, Maven is a critical tool for Java developers, providing a standardized method for building and managing projects. Whether you choose to install it using Homebrew or directly from the Apache source, the process is straightforward and will get you up and running quickly.

Follow-up references

Following is a list of useful references that you can use as a guide in your journey with Java programming, particularly on macOS. These include links to the official documentation of Java, OpenJDK, and Homebrew Java, as well as other additional resources to aid your Java development on macOS. 

OpenJDK documentation

OpenJDK is an open source implementation of the Java Platform, Standard Edition. It is the basis for the Oracle JDK, and it's a great choice for those who prefer open source software.

OpenJDK official documentation: This is the official website of the OpenJDK project. It contains the documentation of OpenJDK, along with other resources like mailing lists, projects, and groups.

Homebrew Java documentation

Homebrew is a package manager for macOS that simplifies the installation of software. It can be used to install Java on a Mac, making it an essential tool for Java developers.

Homebrew Java documentation: This page provides the steps to install OpenJDK using Homebrew. It lists out the commands needed to install Java and provides other related information.

Learn Java application security with cybersecurity lessons on Snyk Learn

Knowledge is power, and this is especially true when it comes to cybersecurity. Snyk Learn provides a platform for developers to gain vital knowledge on securing their applications. With a wide range of cybersecurity lessons, you can learn how to secure your Java applications from the ground up.

Learn Java application security with cybersecurity lessons on Snyk Learn

To sum up, securing your Java applications is not just about fixing known vulnerabilities. It's also about continuously learning and staying updated with the latest security best practices. With Snyk's suite of tools and resources, you can confidently build and deploy secure Java applications.

Patch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo Segment

Snyk is a developer security platform. Integrating directly into development tools, workflows, and automation pipelines, Snyk makes it easy for teams to find, prioritize, and fix security vulnerabilities in code, dependencies, containers, and infrastructure as code. Supported by industry-leading application and security intelligence, Snyk puts security expertise in any developer’s toolkit.

Start freeBook a live demo

© 2024 Snyk Limited
Registered in England and Wales