杰瑞科技汇

mac如何安装Java运行时环境?

Of course! Here's a comprehensive guide to Java Runtime on macOS, covering everything from what it is, how to check if you have it, to how to install and manage different versions.

mac如何安装Java运行时环境?-图1
(图片来源网络,侵删)

What is the Java Runtime Environment (JRE) on a Mac?

The Java Runtime Environment (JRE) is a software package that allows you to run Java applications. Think of it as the "engine" that executes Java code. When you download a program written in Java (like some business applications, Minecraft, or certain development tools), you need the JRE installed on your Mac to make it work.

Important Distinction:

  • JRE (Runtime Environment): For running Java applications. This is what most users need.
  • JDK (Development Kit): For developing Java applications. It includes the JRE plus tools like a compiler (javac) and a debugger. Developers need this.

How to Check if Java is Installed

Before you install anything, it's a good idea to see if Java is already there.

Option A: Check via Terminal (Most Reliable)

Open the Terminal app (you can find it in /Applications/Utilities/ or search for it with Spotlight). Type the following commands:

mac如何安装Java运行时环境?-图2
(图片来源网络,侵删)

Check for the java command: This command checks if the system can find the Java executable.

java -version
  • If Java is installed: You will see output similar to this, showing the version and vendor (like Oracle, OpenJDK, or Apple).
    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)
  • If Java is NOT installed: You will see an error message like:
    zsh: command not found: java

Check for the JDK (Developer Kit): If you're a developer, you'll also want to check for the compiler.

javac -version
  • If you see a version number, the JDK is installed.
  • If you see command not found: javac, you only have the JRE (or nothing at all).

Option B: Check via About This Mac

  • Click the Apple logo () in the top-left corner.
  • Select "About This Mac".
  • Click "System Report...".
  • In the new window, scroll down to the Software section on the left.
  • Look for an entry called "Java" or "Java Runtime". If it's there, it will show the version and location.

How to Install Java on macOS

Apple used to bundle its own version of Java with macOS, but they stopped after Java 6. Modern versions of Java (7, 8, 11, 17, etc.) must be installed manually. The most common and recommended way is by using a package manager or downloading directly from a vendor.

Method 1: Using Homebrew (Recommended for Technical Users)

Homebrew is the most popular package manager for macOS. If you don't have it, installing it is a one-line command.

mac如何安装Java运行时环境?-图3
(图片来源网络,侵删)

Install Homebrew (if you don't have it): Open Terminal and paste this command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Java using Homebrew: Homebrew has a special "tap" (repository) for Java versions.

  • To install the latest LTS (Long-Term Support) version (e.g., Java 21):
    brew install openjdk
  • To install a specific version (e.g., Java 17, which is very common):
    brew install openjdk@17

    After installing a specific version, you'll need to create a symlink to make it the default.

    sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

Method 2: Downloading Directly from a Vendor (Recommended for Most Users)

This method gives you more control and is straightforward.

The best vendors for Open Source Java are:

  • Eclipse Temurin (by Adoptium): A community-driven, high-quality build of OpenJDK. This is often the top recommendation.
  • Amazon Corretto: A no-cost, multi-platform, long-term support distribution of the OpenJDK.
  • Oracle JDK: The official build from Oracle. It has different licensing terms for commercial use, but it's free for personal use and development.

Step-by-Step Example with Eclipse Temurin:

  1. Go to the Eclipse Temurin download page.
  2. Select the version you need (e.g., LTS for long-term support is recommended).
  3. Under "Operating System," select macOS.
  4. Under "Architecture," select x64 (for Intel Macs) or aarch64 (for Apple Silicon/M1/M2/M3 Macs).
  5. Click the download link for the installer (.pkg) file.
  6. Open the downloaded .pkg file and follow the on-screen instructions from the installer.
  7. The installer will place Java in the standard system location (/Library/Java/JavaVirtualMachines/).

Managing Multiple Java Versions

It's common to need different Java versions for different projects (e.g., Java 17 for a legacy project and Java 21 for a new one).

How to Switch Between Versions

After installing multiple versions (e.g., Java 17 and Java 21), you can switch which one your system uses by default.

List Installed Versions: Java installations on macOS create symbolic links in /usr/local/bin. You can see what's available:

ls /usr/local/bin/java*

You might see java, java17, java21, etc.

Set the Default Version: Use the update-alternatives tool (common on Linux, but works well on macOS with Homebrew Java) or, more simply, just create your own symlink.

The easiest way is to add the path to your desired version to the PATH environment variable in your shell's configuration file (.zshrc for modern macOS).

  • To make Java 21 the default:
    echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
  • To switch back to Java 17:
    echo 'export PATH="/usr/local/opt/openjdk@17/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
  • To check the active version:
    java -version

Important: Apple's Legacy Java 6

If you are working with very old software that requires Java 6, you can still install it.

  • Download: Get the "Java for macOS 2025-001" package from Apple's support site. Download here.
  • Install: Run the downloaded .dmg and follow the instructions.
  • Usage: This version of Java is in a separate location (/System/Library/Java/JavaVirtualMachines/). You can explicitly call it by its full path:
    /usr/libexec/java_home -V

    This command will list all available Java homes. You can then use one of them directly:

    /Library/Java/JavaVirtualMachines/jdk1.6.0_65.jdk/Contents/Home/bin/java -version

Summary

Task Action Command / Method
Check if Java is installed In Terminal java -version
Install Java (Recommended) Download from Eclipse Temurin or Amazon Corretto Download .pkg file and run installer
Install Java (Technical) Use Homebrew brew install openjdk
Switch Java versions Modify the PATH environment variable Edit ~/.zshrc file
Use old Java 6 software Download legacy package from Apple Apple Support Link
分享:
扫描分享到社交APP
上一篇
下一篇