If you’re starting your journey in Java development and you’re using a Mac, one of the first things you’ll need to do is set up your Java environment properly. This means installing the Java Development Kit (JDK), setting environment variables (like
JAVA_HOME
), and making sure everything works smoothly from your terminal and your favorite code editor or IDE.
In this step-by-step guide, you’ll learn how to set up the Java environment on
macOS—the right way. Whether you're new to programming or just switching to a Mac, this article will help you get Java up and running in no time.
What You’ll Need
Before we dive in, here’s what we’ll be doing:
- Check if Java is already installed
- Install a Java Development Kit (JDK)
- Set the
JAVA_HOME
environment variable
- Verify the installation
- (Optional) Install a code editor or IDE
Let’s get started!
Step 1: Check if Java Is Already Installed
Your Mac might already have a version of Java installed—especially if it’s older or used by other apps.
To check, open your Terminal and run:
java -version
If you see something like:
java version "17.0.2" 2022-01-18 LTS
Great! You already have Java. But if you see:
No Java runtime present, requesting install.
...then it’s time to install it.
Step 2: Install the Java Development Kit (JDK)
You’ll want the
JDK (not just the JRE), because it includes everything needed to write and run Java code.
Option 1: Use Homebrew (Recommended)
If you don’t have Homebrew yet, install it first by running this in Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, install the latest version of OpenJDK:
brew install openjdk@21
You can replace 21
with any version you want (like 17
, 20
, etc.), depending on your needs.
After the installation finishes, you’ll see a note from Homebrew. It usually includes instructions like this:
To use the JDK, add the following to your .zprofile or .zshrc:
export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"
export CPPFLAGS="-I/opt/homebrew/opt/openjdk@21/include"
Copy and paste those lines into your .zshrc
file (or .bash_profile
if you're using bash):
nano ~/.zshrc
Paste the lines at the bottom, save (
Ctrl + O
), and exit (
Ctrl + X
).
Apply the changes:
source ~/.zshrc
Option 2: Download from Oracle or Other Providers
You can also download the JDK manually:
Download the
.pkg
file for macOS and follow the installer instructions.
Step 3: Set the JAVA_HOME
Environment Variable
After installation, it’s important to set the
JAVA_HOME
variable. This tells tools and build systems (like Maven or Gradle) where Java is located on your machine.
In your terminal, add the following line to your
.zshrc
or
.bash_profile
:
export JAVA_HOME=$(/usr/libexec/java_home)
Then reload your shell:
source ~/.zshrc
To check if it’s working:
echo $JAVA_HOME
You should see something like:
/Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home
Step 4: Verify the Installation
Run these commands to make sure Java is working properly:
java -version
javac -version
The
java
command runs the Java runtime, while
javac
is the Java compiler.
If you see version numbers for both, you're all set!
Step 5: (Optional) Install an IDE or Code Editor
Now that Java is installed, you’ll probably want to write code in a nice editor. Here are some great options:
1. IntelliJ IDEA
2. Visual Studio Code
- Lightweight and beginner-friendly
- Install Java extensions for syntax highlighting, building, and running Java code
- Download from code.visualstudio.com
3. Eclipse IDE
- Another classic Java IDE, especially for enterprise development
- Download from eclipse.org
Any of these will work great—pick the one that feels right for you.
image quote pre code