---
title: "How to Install Java on Mac"
description: "Learn how to install Java on Mac securely and safely with either OpenJDK or Oracle Java to run all of your Java applications!"
canonical: "https://adamtheautomator.com/install-java-on-mac/"
---

# How to Install Java on Mac

> Learn how to install Java on Mac securely and safely with either OpenJDK or Oracle Java to run all of your Java applications!

Source: https://adamtheautomator.com/install-java-on-mac/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Install Java on Mac](https://adamtheautomator.com/wp-content/uploads/2022/11/install-java-on-mac.jpg)

# How to Install Java on Mac

[![](https://secure.gravatar.com/avatar/1bc5e1d52466fc3739f550c8d78be310684747bf1466a98d698320627ea243e2?s=192&d=mm&r=g)Michael Nguyen Tu](https://adamtheautomator.com/author/michael-nguyen-tu/)30 November 20227 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Java](/tag/java/)[macOS](/tag/macos/)

Table of Contents

*   [Prerequisite](#prerequisite)
*   [Using Homebrew to Install Java on Mac](#using-homebrew-to-install-java-on-mac)
*   [Installing the Latest Version](#installing-the-latest-version)
*   [Installing an Older or a Specific Version](#installing-an-older-or-a-specific-version)
*   [Installing Java via the DMG Package](#installing-java-via-the-dmg-package)
*   [Switching Between Java Versions](#switching-between-java-versions)
*   [Creating Your First Java Project on Mac](#creating-your-first-java-project-on-mac)
*   [Conclusion](#conclusion)

Unsure how to install Java on Mac? As a developer, having different programming languages, like Java, installed on your computer is crucial. Java, one of the most popular programming languages, lets you develop web, mobile, and desktop applications.

In this tutorial, you will learn to install Java on a Mac in many ways and create a simple project to get you started in your Java journey.

Read on to expand your programming language repertoire!

## Prerequisite

This tutorial comprises of hands-on demonstration. To follow along, ensure you have the following:

A Mac computer – This tutorial uses Big Sur, but any Mac running macOS 11.0 or higher will work.

Homebrew installed.

Related:[How to Install Homebrew on macOS](https://adamtheautomator.com/install-homebrew/)

A code editor – This tutorial uses Visual Studio Code (VS Code), but any code editor will work.

_Related:_ [How to Install Visual Studio Code on Mac](https://adamtheautomator.com/visual-studio-code-on-mac/)

Related:[How to Install Visual Studio Code on Mac](https://adamtheautomator.com/visual-studio-code-on-mac/)

## Using Homebrew to Install Java on Mac

One of the many ways you can install Java on Mac is by using Homebrew, a package manager for macOS. Homebrew lets you quickly install various software, like Java, on your Mac with a few commands.

Homebrew is the right package manager for you if you are more of a command-line person. And in this tutorial, you will install the latest and old/specific versions of Java on Mac.

### Installing the Latest Version

Installing multiple versions of Java on your Mac works without any conflict. But the latest version is always recommended as it contains the latest security updates.

To install the latest version of Java on your Mac:

1\. Run the below [brew update](https://osxdaily.com/2021/02/13/how-update-homebrew-mac/) command to update Homebrew.

```bash
brew update
```

![Updating Homebrew](https://adamtheautomator.com/wp-content/uploads/2022/11/image-449.png)

Updating Homebrew

2\. Next, run the following command to [search](https://dev.to/andremare/homebrew---basics--cheatsheet-3a3n#brew-search-install-remove) for the available java formula. Homebrew uses formulas to manage packages and applications.

```bash
brew search java
```

You can see below that there are many versions of Java available. The java formula is the alias for the most recent release.

![Searching for the available Java formula](https://adamtheautomator.com/wp-content/uploads/2022/11/image-450.png)

Searching for the available Java formula

3\. Run the below command to get detailed information (info) about the java formula.

```bash
brew info java
```

As you can see below, at this time of writing, the latest Java release is 19, which is not installed yet.

![Getting detailed information about the java formula](https://adamtheautomator.com/wp-content/uploads/2022/11/image-451.png)

Getting detailed information about the java formula

4\. Now, run the following brew install command to install java on your Mac.

```bash
brew install java
```

![Installing the latest Java release](https://adamtheautomator.com/wp-content/uploads/2022/11/image-452.png)

Installing the latest Java release

5\. Once installed, run the below ln command to create a symbolic link (symlink) that points to the installed Java version.

In the command below, the following paths are as follows:

*   /usr/local/opt/openjdk/libexec/openjdk.jdk – The created symlink.
*   /Library/Java/JavaVirtualMachines/openjdk.jdk – The path where the symlink points to.

Creating a symlink is necessary as some applications only work correctly if they can find a Java installation in the standard location.

```bash
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
```

Enter your password when prompted to complete the process.

![Creating a symlink for Java](https://adamtheautomator.com/wp-content/uploads/2022/11/image-453.png)

Creating a symlink for Java

6\. Finally, run the below command to print the installed Java version.

```bash
java --version
```

The output below confirms you have successfully installed the latest version of Java on your Mac, where:

*   OpenJDK is an open-source implementation of the Java platform.
*   JDK is the Java Development Kit, a set of tools for developing Java applications.

Congratulations! You have successfully installed the latest Java release on your Mac.

![Checking the installed Java version](https://adamtheautomator.com/wp-content/uploads/2022/11/image-454.png)

Checking the installed Java version

### Installing an Older or a Specific Version

The latest version of your Java installation should be fine and all. But “version compatibility” is a thing when building or developing programs. The good news is that you can install older or specific versions of Java alongside other versions.

To install an older or a specific Java version, you first have to find the available ones:

1\. Run the below command to search the openjdk formula for all available Java versions. This formula usually contains much older versions of Java than the java formula.

```bash
brew search openjdk
```

Pick one from the available versions below (openjdk@8, 11, and 17) that you wish to install.

> 💡 _The openjdk@17 is the newest LTS version, and some applications might not support it yet._

![Searching for available Java versions](https://adamtheautomator.com/wp-content/uploads/2022/11/image-455.png)

Searching for available Java versions

2\. Next, run the below command to install a specific Java version, replacing <java\_version> with your preferred one.

```bash
brew install <java_version>
```

As you see below, this tutorial’s choice is the openjdk@11 version since openjdk@8 is too old and is not recommended anymore. 

![Installing an older version of Java](https://adamtheautomator.com/wp-content/uploads/2022/11/image-456.png)

Installing an older version of Java

3\. Once the installation completes, run the below command, which does not provide output but creates a symlink.

```bash
sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
```

4\. Finally, run the exact java command below to verify the version of your Java installation.

```bash
java --version
```

![Verifying the installed Java version](https://adamtheautomator.com/wp-content/uploads/2022/11/image-457.png)

Verifying the installed Java version

## Installing Java via the DMG Package

If you fancy a GUI instead of a command-line environment, you want to try installing Java with a DMG package (a disk image file). A DMG package acts as a virtual optical disc containing compressed software installation files and other data like bootable images.

Installing Java via a DMG package is a little bit more manual work. But hey, clicking through a basic installation wizard would not hurt.

1\. Open your favorite web browser, and navigate to Oracle’s official [Java downloads page](https://www.oracle.com/java/technologies/downloads/#jdk19-mac).

2\. Next, click on the DMG link for the JDK version you want to install to download its DMG package to your _Downloads_ directory. The tutorial goes with Java 19, the latest version at the time of this writing.

![Downloading Java’s DMG package](https://adamtheautomator.com/wp-content/uploads/2022/11/image-458.png)

Downloading Java’s DMG package

3\. Once downloaded, double-click the DMG file to launch the installer.

![Launching the Java installer](https://adamtheautomator.com/wp-content/uploads/2022/11/image-459.png)

Launching the Java installer

4\. On the Introduction tab, click Continue to acknowledge the welcome page.

![Acknowledging the welcome page](https://adamtheautomator.com/wp-content/uploads/2022/11/image-460.png)

Acknowledging the welcome page

5\. Now, click Install to the installation process.

![Installing Java on Mac](https://adamtheautomator.com/wp-content/uploads/2022/11/image-461.png)

Installing Java on Mac

6\. Enter your password when prompted and click Install Software to authorize the installation.

![Authorizing the installation](https://adamtheautomator.com/wp-content/uploads/2022/11/image-462.png)

Authorizing the installation

7\. Once installed, click Close to close the installation wizard.

And that is it! You have now successfully installed Java on your Mac via the DMG package.

![Click Close to close the installer.](https://adamtheautomator.com/wp-content/uploads/2022/11/image-463.png)

Click Close to close the installer.

8\. Finally, run the below command for the last time to check the installed Java version.

```bash
java -version
```

![Verifying the installation](https://adamtheautomator.com/wp-content/uploads/2022/11/image-464.png)

Verifying the installation

## Switching Between Java Versions

Suppose one project requires Java 11, but another requires Java 19. If so, how do you switch between the two Java versions? Worry not! You can work on different Java versions, switching between each, on your Mac at will.

To switch between Java versions:

1\. Run the below command to find out the available versions of Java installed on your computer.

```bash
/usr/libexec/java_home -V
```

Pick one version you want to switch to, as shown below.

![Listing all available versions of Java](https://adamtheautomator.com/wp-content/uploads/2022/11/image-465.png)

Listing all available versions of Java

2\. Next, run the below command to find the current default Java version.

```bash
/usr/libexec/java_home
```

As you can see below, Oracle JDK 19 is currently the active or default Java version.

![Finding the current default Java version](https://adamtheautomator.com/wp-content/uploads/2022/11/image-466.png)

Finding the current default Java version

3\. Run the below echo command to find the current shell ($SHELL).

```bash
echo $SHELL
```

Notice in the output below that this tutorial uses the ZSH shell.

![Finding the current shell](https://adamtheautomator.com/wp-content/uploads/2022/11/image-467.png)

Finding the current shell

Related:[Install Oh My Zsh on Ubuntu for a Next Level Command Line](https://adamtheautomator.com/install-oh-my-zsh-on-ubuntu/)

4\. Now, run the below command to switch to a different Java version, replacing <version> with your preferred version.

These commands perform the following:

*   export the path of a specific Java version to the JAVA\_HOME environment variable, which tells Java to use the version you specified.
*   source the profile and apply the changes.

> 💡 _Perhaps you are on a Bash shell. If so, run the source ~/.bashrc command instead to source the profile._

Related:[How to Get Started with Git Bash on Windows](https://adamtheautomator.com/git-bash/)

```bash
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
```

> 💡 _Switching to another Java version does not provide output, but you will later verify if the switch is a success._

5\. Finally, run the below commands to verify the current Java -version.

```bash
echo $JAVA_HOME
java -version
```

The output below confirms the version switch is a success, as the current version is set to OpenJDK 11.

![Verifying the version switch](https://adamtheautomator.com/wp-content/uploads/2022/11/image-468.png)

Verifying the version switch

## Creating Your First Java Project on Mac

Successfully installing Java and switching between versions do not tell if Java is working correctly. How do you test if Java works on your Mac? You will create a basic “hello world” program and use Java to compile and run the program.

To create a Java project:

1\. Run the following commands to create a directory (mkdir) for your project (hello-world), and change the working directory (cd) into that directory.

These commands do not provide output, but you will verify the project files in the following steps.

```bash
# Create the hello-world directory
mkdir hello-world
# Change the working directory
cd hello-world
```

2\. Next, create a file named _HelloWorld.java_ in your preferred code editor, add the code below to the file, save the changes and close the file.

The code below is a simple Java program that prints the Hello World message to the console.

```java
# Create the hello-world directory
mkdir hello-world
# Change the working directory
cd hello-world
```

3\. Run the following [javac](https://docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html) command, which does not provide output, but compiles your program (_HelloWorld.java_).

```java
javac HelloWorld.java
```

4\. Now, run the command below to list (ls) all contents (-la) of the working directory (_~/hello-world_).

```bash
ls -la
```

You will see a new file named _Hello.class_ is generated, as shown below. This file contains the bytecode for your program.

![Verifying the project files](https://adamtheautomator.com/wp-content/uploads/2022/11/image-469.png)

Verifying the project files

5\. Lastly, execute the below command to run your program (Hello).

This command calls the Java Runtime Environment to execute your program, where the Hello class is loaded, and the main method is executed.

```java
java Hello
```

The output below confirms that your program is working as expected and your Java installation is successful.

![Running the Java program](https://adamtheautomator.com/wp-content/uploads/2022/11/image-470.png)

Running the Java program

## Conclusion

In this tutorial, you have learned many ways how to install Java on Mac and create a simple Java project. Java is one of the most popular programming languages in the world. Every nook and corner of the tech industry uses Java in some way or the other.

And at this point, you can now confidently build your projects. Moreover, since you can quickly switch between versions, you never have to worry about version compatibility.

With this newfound knowledge, why not [build your own Java application](https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html) and see how it works?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-java-on-mac%2F&text=How%20to%20Install%20Java%20on%20Mac)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Finstall-java-on-mac%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-java-on-mac%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/11/How-to-Install-NitroShare-for-Cross-Platform-File-Transfer.jpg)

### [Install NitroShare and Streamline Your File Transfers Today](/nitroshare/)

Simplify cross-platform file transfers with NitroShare. This tutorial guides you through installation and use for fast, effortless file sharing between different operating systems.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/How-to-Install-Homebrew-on-macOS.jpg)

### [Install Homebrew on macOS: A Step-by-Step Tutorial](/install-homebrew/)

Learn how to use package management from the command-line and install Homebrew on macOS to easily install packages!

![](https://adamtheautomator.com/wp-content/uploads/2022/10/Unleash-macOS-Virtualization-With-VMware-Fusion.jpg)

### [Maximize Your macOS Virtual Experience with VMware Fusion](/vmware-fusion/)

Learn to utilize macOS virtualization with the VMware Fusion solution to develop and test locally in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
