Of course! Here is a comprehensive guide to getting started with Java on macOS, covering everything from installation to development and running your first program.

Summary: The Modern Way (Recommended)
For most users in 2025 and beyond, the best way to use Java on a Mac is by installing Homebrew and then using it to install Adoptium Temurin (Eclipse Temurin).
- Why? It's the most reliable, up-to-date, and community-supported method. It gives you a standard, OpenJDK-based version of Java that works seamlessly with all modern tools.
- What is Homebrew? It's the de-facto package manager for macOS. Think of it as the "App Store" for developer command-line tools.
- What is Temurin? It's a high-quality, open-source build of Java, managed by the Eclipse Foundation. It's the recommended distribution by the Java community.
Step 1: Install Homebrew (If you don't have it)
Homebrew makes installing Java incredibly simple.
-
Open the Terminal. You can find it in
Applications/Utilities, or just pressCmd + Spaceto open Spotlight and type "Terminal". -
Run the official Homebrew installer. Copy and paste this command into your Terminal and press Enter.
(图片来源网络,侵删)/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Follow the on-screen instructions. It will ask for your password and may take a few minutes to complete.
-
(For Apple Silicon Macs only) After installation, you may need to run this command to add Homebrew to your PATH:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc eval "$(/opt/homebrew/bin/brew shellenv)"
Note: Modern macOS versions use Zsh as the default shell. If you use Bash, the file path might be
~/.bash_profileinstead.
Step 2: Install Java using Homebrew
Now that Homebrew is ready, installing Java is a single command.

-
In your Terminal, run:
brew install temurin
-
Wait for the installation to finish. Homebrew will download and install the latest Long-Term Support (LTS) version of Java.
Step 3: Verify Your Java Installation
This is a crucial step to ensure everything is working correctly.
-
Check the Java version. Run the following command:
java -version
You should see output similar to this, indicating the version and vendor:
openjdk version "17.0.8" 2025-07-18 OpenJDK Runtime Environment Temurin-17.0.8+7 (build 17.0.8+7) OpenJDK 64-Bit Server VM Temurin-17.0.8+7 (build 17.0.8+7, mixed mode, sharing) -
Check the Java compiler (javac). This confirms the full Development Kit (JDK) is installed.
javac -version
You should see:
javac 17.0.8 -
Find where Java is installed. This is useful for configuring other tools.
/usr/libexec/java_home -V
This will list all Java installations Homebrew has made. The output will look something like this, and you can use the path (e.g.,
/opt/homebrew/Cellar/openjdk@17/17.0.8) for advanced configurations.
Step 4: Set Up Your Development Environment
Now you need a place to write your code and a tool to compile and run it. You have two main options:
Option A: The Simple Way (Using a Text Editor)
This is perfect for beginners.
-
Choose a Text Editor:
- Visual Studio Code (VS Code): Extremely popular, free, and has fantastic Java support.
- IntelliJ IDEA Community Edition: A full-featured, powerful IDE from JetBrains. It's the gold standard for Java development and is also free.
-
Install VS Code:
- Download it from the official website.
- Open VS Code.
- Go to the Extensions tab (the icon with four squares on the left sidebar).
- Search for and install the "Extension Pack for Java" by Microsoft. This bundle includes everything you need for Java development.
Option B: The Powerful Way (Using an IDE)
-
Install IntelliJ IDEA:
- Download the Community Edition from the official JetBrains website.
- Drag the application into your
Applicationsfolder.
-
Configure IntelliJ to use your new Java:
- Open IntelliJ IDEA.
- Go to
IntelliJ IDEA->Settings...(orPreferences...on older versions). - Navigate to
Build, Execution, Deployment->Build Tools->Gradle(orMaven, depending on your project). - In the Gradle JDK dropdown, select the version of Java you just installed (it should be named something like "17 (Temurin)").
- Click OK.
Step 5: Write and Run Your First "Hello, World!" Program
Let's do it from the command line first to understand the basics.
-
Create a Project Directory:
mkdir java-projects cd java-projects
-
Create the Java File: Use a simple text editor (like
nanoin the terminal, or VS Code) to create a file namedHelloWorld.java.public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } -
Compile the Code: In your terminal, use the
javac(Java Compiler) command.javac HelloWorld.java
This will create a new file named
HelloWorld.classin the same directory. This is the Java bytecode that the Java Virtual Machine (JVM) can execute. -
Run the Program: Use the
javacommand to run your compiled code.java HelloWorld
Important: Notice you do not include the
.classextension when running the program.You should see the output:
Hello, World!
Common Issues and Troubleshooting
-
Problem:
'java' or 'javac' is not recognized...- Cause: Your system's
PATHenvironment variable doesn't point to the Java bin directory. - Fix: Homebrew usually handles this automatically. If not, run
/usr/libexec/java_home -vto get the path to your Java installation, then add thebindirectory from that path to your shell's configuration file (.zshrcor.bash_profile). For example:export PATH="/opt/homebrew/opt/openjdk@17/bin:$PATH".
- Cause: Your system's
-
Problem: The wrong version of Java is running.**
- Cause: You might have an older version of Java from Apple or another source that takes precedence.
- Fix: Use
/usr/libexec/java_home -vto see all versions. You can then set the active version for your current terminal session with:export JAVA_HOME=$(/usr/libexec/java_home -v "17"). To make it permanent, add that line to your.zshrcfile.
-
Problem: My IDE (like VS Code) can't find Java.**
- Cause: The IDE's language server isn't configured correctly.
- Fix: In VS Code, open the Command Palette (
Cmd+Shift+P), typeJava: Update Javaand select the correct JDK version you installed with Homebrew. For IntelliJ, follow the configuration steps in Step 4B.
