How to setup Java on macOS

Java is a famous high-level programming language created in 1990 at Sun Microsystems by James Gosling and his colleagues.  Java is known as WORA (write once, run anywhere) terminology, which enhances that the Java program can be executed on any platform with JVM (Java virtual machine).

Java programming language is used to develop various mobile, desktop, gaming, and business applications. It is also used to develop web frameworks like Spring and Struts.

The syntax of Java programming language is close to the syntax of C++ programming language; this may be helpful for C++ programmers to learn Java efficiently.  Java is a highly adapted programming language for various business models like healthcare, banking, and education. 

Setting up Java on macOS using the command line

First, we need to open the terminal application on our Mac. We can go to our terminal by pressing the CMND + space bar keys. When we reach the Spotlight search bar, write the terminal and press “Enter” to open it.

Spotlight Search
Spotlight Search

Before installation, please ensure that no pre-mount file of the JDK version is available on the system. To check that, we'll open the "Finder" on our Mac, and if the highlighted area in the following image appears on our Mac, click the arrow button to eject the file.

To install Java on macOS using the command line, copy and paste the commands into the terminal.

# Download latest version of Java
curl -LO "https://download.oracle.com/java/17/latest/jdk-17_macos-aarch64_bin.dmg"
# Mount the DMG file
hdiutil mount jdk-17_macos-aarch64_bin.dmg
# Run the Java installer
sudo installer -pkg /Volumes/JDK\ 17.0.6/JDK\ 17.0.6.pkg -target /
# Check the installed Java version
java -version

Here's a step-by-step explanation for installing Java on macOS using the command line:

  • Line 2: We download the latest version of Java from the Oracle website. Here’s the command to download Java SE Development Kit 17 for macOS. This will download the Java installation file in our current directory.

  • Line 5: Mount the DMG file once the download is complete. This will create a new volume containing the Java installer.

  • Line 8: Next, run the Java installer. This will start the Java installation process. It may be prompted to enter a system password to complete the installation.

  • Line 11: When the installation is completed, we must verify the successful installation of Java.

Setting up Java on macOS using Homebrew in the command line

To install Java on MacOS using the Homebrew in the command line, copy the commands and paste them into the terminal.

# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Update Homebrew
brew update
# Available versions of Java
brew search java
# Install Java
brew install java11
# Check the installed Java version
java -version

Here's a step-by-step explanation for installing Java on macOS using the Homebrew in the command line:

  • Line 2: We Install Homebrew on the macOS.

  • Line 5: After the successful Homebrew installation, we should update it to ensure that we have the latest version of Homebrew. This will help us install the latest Java version on our macOS.

  • Line 8: We can search for available versions of Java before installing. This will output a list of available versions of Java that we can install with Homebrew.

  • Line 11: After deciding on the necessary Java version, we may install it. We only needed to mention the version in the command to install the Java version that we needed to. We'll install version 11 of Java.

  • Line 14: When the installation is completed, we must verify the successful installation of java.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved