Home » Blog » How to Install Java on Mac: Step-by-Step Guide
How to install Java on macos

How to Install Java on Mac: Step-by-Step Guide

If you’ve ever tried running a Java program on a Mac without setting anything up, you’ve probably seen an error about a missing Java Runtime. That’s because, unlike JavaScript, Java doesn’t come built into your computer — you need to install a Java Development Kit (JDK) first.

In this guide, we’ll go step by step:

  • Check if Java is already installed (so you don’t install it twice by mistake)
  • Learn what a JDK kit is and why most developers use it
  • Install Java on a Mac using either Homebrew or the official Oracle download
  • Set up your environment so the java command works anywhere in your terminal
  • Verify the installation, uninstall if needed, and fix common issues

Whether you’re new to Java or just coming from another language like JavaScript, this guide will give you a clear path to getting Java up and running on macOS.

Check if Java is Already Installed

Before installing Java, it’s worth checking if it’s already on your Mac. Maybe you’re not on your personal machine, or you installed it months ago for a side project and completely forgot.

To check if Java is already installed:

  1. Open your Terminal
  2. Type java -version
  3. If Java is installed, you’ll see something like:
OpenJDK version 11.x
Bash

If it’s not installed, you’ll get a message like:

The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
Bash

If you see the “Unable to locate” message, don’t worry — just continue with this guide to install Java on your Mac.

What is a JDK?

If this is your first programming language, or if you’re like me, coming from another one — in my case, Javascript — you might not know much about the Java ecosystem. And honestly, you don’t really need to at first, right?

Still, because of assumptions, you might expect the installation to be as simple as running brew install java. But when you look it up, you’ll quickly see that the recommended way is to install a JDK instead. Before we continue, let’s take a quick look at what that actually is.

JDKJava Development Kit is an open-source implementation of Java, maintained by big tech companies such as Oracle, Red Hat, and others.

When we refer to an open-source implementation, it simply means software built according to official rules. In this case, the rules come from the Java SE Specification.

So what does the JDK provide? It contains:

  • Compiler – turns your .java files into an executable form called bytecode.
  • Runtime (JVM) – executes that bytecode on any computer (Windows, Mac, Linux, etc.).
  • Standard library – a huge set of built-in tools to help you out with your code.

It essentially has everything you need to use Java on your machine. The JDK is considered an essential tool for developers working with Java, and the OpenJDK project and Oracle distribute it.

Install Java on Mac

You can install Java on macOS in two main ways:

  1. Install Using Homebrew – quick and beginner-friendly
  2. Install by Downloading from Oracle

If you’re new to Java or simply want the fastest setup, the Homebrew method is the best choice. (We have a separate post that walks you through installing Homebrew if you don’t have it yet.)

In this guide, we’ll use the Homebrew method.

To find out which is the latest version of Java, you can visit this page.

Install Java using Homebrew

To install Java using Homebrew:

  1. Update Homebrew
    Open your terminal and run: brew update
    This makes sure you’re working with the latest package list.
  2. Install OpenJDK 21
    After updating, run:
    brew install openjdk@21
  3. Verify installation
    Open your terminal and run:
java -version
Bash

At the time of writing this guide, version 21 is the latest long-term support (LTS) release. If you want to install a different version, just replace 21 with the version number you need (for example, openjdk@17).

Optional: Read More About the Package
You can check out the official Homebrew formula page for OpenJDK here.

Set JAVA_HOME Environment Variable

After installing OpenJDK, you could have tried to verify the installation and found out that you still get

The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
Bash

In that case, you will need to add the Java home directory to your PATH. Since we installed OpenJDK@21, the command to add this to our PATH is

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

You’ll also see this message in the Homebrew installation logs for OpenJDK, near the end of the process.

What this command essentially does is prepend the existing list of paths with the directory where OpenJDK is installed. So when you ask it to run a java program, it will search first there.

If you installed a different version, change the version accordingly to the command above.

Verify Installation

To verify our installation regardless of the method we used we should type java --version. By now you should be seeing

  ~ java -version
openjdk version "21.0.8" 2025-07-15
OpenJDK Runtime Environment Homebrew (build 21.0.8)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.8, mixed mode, sharing)
Bash

Uninstalling Java (Homebrew)

If you want to remove a specific version of OpenJDK installed via Homebrew, run:

brew uninstall openjdk@21
Bash

Checking Installed Versions

To see all the Java versions (and other packages) you currently have installed, run:

brew list --versions
Bash

You might see something like this:

...
openjdk@17 17.0.12
openjdk@21 21.0.4
...
Bash

This means you have both Java 17 and Java 21 installed on your system.

Choosing an IDE for Java

Now that you’ve installed Java, the next step is picking an Integrated Development Environment (IDE) to support your learning. An IDE helps you compile code, catch mistakes, and debug programs more easily — making your path to mastering Java smoother.

Some popular free choices include:

  • IntelliJ IDEA Community Edition, published by JetBrains. This is my personal recommendation, since it’s beginner-friendly and offers a free edition.
  • Eclipse, maintained by the Eclipse Foundation
  • NetBeans, maintained by the Apache Foundation

Pick one of these tools, set it up, and you’ll be ready to start coding your very first Java programs with confidence!

Troubleshooting Common Issues

Below are some common issues you might face when installing or using Java on macOS.
If you’re dealing with a different problem and haven’t figured out the solution, drop it in the comments — we’ll try to help you out.
Also, if you’ve solved a tricky Java issue yourself, share it in the comments so other readers can benefit too.

Wrong Java Version Running

Cause: Multiple Java versions are installed, and the wrong one is first in your PATH.
Fix: Check which Java is being used

which java
Bash

Then adjust your PATH so the version you want comes first. You can do that by typing:

export PATH="/opt/homebrew/opt/openjdk@<version-you-want>/bin:$PATH"
Bash

brew install fails

Cause: Homebrew may be outdated or missing.

Fix: Update Homebrew:

brew update
Bash

If Homebrew is not installed, see our homebrew installation guide.

java Command Not Found

Cause: OpenJDK is installed but not linked to your environment.

Fix: Link the installed version so the java command is available:

brew link --force --overwrite openjdk@21
Bash

If you installed a different version, replace 21 with that version number.

Leave a reply

Your email address will not be published. Required fields are marked *