杰瑞科技汇

Java在Mac OS上怎么安装配置?

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.

Java在Mac OS上怎么安装配置?-图1
(图片来源网络,侵删)

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.

  1. Open the Terminal. You can find it in Applications/Utilities, or just press Cmd + Space to open Spotlight and type "Terminal".

  2. Run the official Homebrew installer. Copy and paste this command into your Terminal and press Enter.

    Java在Mac OS上怎么安装配置?-图2
    (图片来源网络,侵删)
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  3. Follow the on-screen instructions. It will ask for your password and may take a few minutes to complete.

  4. (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_profile instead.


Step 2: Install Java using Homebrew

Now that Homebrew is ready, installing Java is a single command.

Java在Mac OS上怎么安装配置?-图3
(图片来源网络,侵删)
  1. In your Terminal, run:

    brew install temurin
  2. 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.

  1. 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)
  2. Check the Java compiler (javac). This confirms the full Development Kit (JDK) is installed.

    javac -version

    You should see:

    javac 17.0.8
  3. 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.

  1. 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.
  2. 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)

  1. Install IntelliJ IDEA:

  2. Configure IntelliJ to use your new Java:

    • Open IntelliJ IDEA.
    • Go to IntelliJ IDEA -> Settings... (or Preferences... on older versions).
    • Navigate to Build, Execution, Deployment -> Build Tools -> Gradle (or Maven, 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.

  1. Create a Project Directory:

    mkdir java-projects
    cd java-projects
  2. Create the Java File: Use a simple text editor (like nano in the terminal, or VS Code) to create a file named HelloWorld.java.

    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }
  3. Compile the Code: In your terminal, use the javac (Java Compiler) command.

    javac HelloWorld.java

    This will create a new file named HelloWorld.class in the same directory. This is the Java bytecode that the Java Virtual Machine (JVM) can execute.

  4. Run the Program: Use the java command to run your compiled code.

    java HelloWorld

    Important: Notice you do not include the .class extension 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 PATH environment variable doesn't point to the Java bin directory.
    • Fix: Homebrew usually handles this automatically. If not, run /usr/libexec/java_home -v to get the path to your Java installation, then add the bin directory from that path to your shell's configuration file (.zshrc or .bash_profile). For example: export PATH="/opt/homebrew/opt/openjdk@17/bin:$PATH".
  • 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 -v to 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 .zshrc file.
  • 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), type Java: Update Java and select the correct JDK version you installed with Homebrew. For IntelliJ, follow the configuration steps in Step 4B.
分享:
扫描分享到社交APP
上一篇
下一篇