Install OpenJDK on Windows – 在 Windows 上安装 OpenJDK

最后修改: 2024年 2月 24日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Java plays a pivotal role in modern software development, empowering many applications and systems. To harness the power of Java on our machine, we need to install the Java Development Kit (JDK). While Oracle JDK is a popular choice, OpenJDK offers an open-source alternative with similar functionality.

Java 在现代软件开发中发挥着举足轻重的作用,为许多应用程序和系统提供了强大的支持。要在机器上利用 Java 的强大功能,我们需要安装 Java 开发工具包 (JDK)。尽管 Oracle JDK 是一种流行的选择,OpenJDK 也提供了一种具有类似功能的开源替代方案。

In this article, we’ll explore various approaches to installing OpenJDK on a Windows environment, catering to different preferences and requirements.

在本文中,我们将探讨在 Windows 环境中安装 OpenJDK 的各种方法,以满足不同的偏好和要求。

2. Manual Installation

2.手动安装

This method involves downloading the OpenJDK distribution directly from the official website or trusted repositories such as AdoptOpenJDK.

这种方法包括直接从 官方网站或可信软件源(如 AdoptOpenJDK )下载 OpenJDK 发行版。

Once downloaded, extract the archive’s contents to a preferred location on our machine. It’s essential to configure environment variables such as PATH and JAVA_HOME to point to the directory where OpenJDK is installed. Let’s proceed by accessing the control panel and navigating to System settings:

下载完成后,将压缩包的内容解压缩到我们机器上的首选位置。必须配置 PATHJAVA_HOME 等环境变量,使其指向安装 OpenJDK 的目录。让我们继续访问控制面板并导航至 System settings

System Settings

Selecting the Advanced system settings will prompt a dialog box to appear:

选择高级系统设置将提示出现一个对话框:

Advanced Settings

Let’s see the system and user variables now by clicking on Environment Variables. Here, we’ll modify the PATH variable and include the JAVA_HOME variable. The JAVA_HOME variable should indicate the installation directory of OpenJDK, while the PATH variable should direct to the bin directory of the JDK.

现在,让我们点击环境变量来查看系统和用户变量。在这里,我们将修改 PATH 变量,并包含 JAVA_HOME 变量。JAVA_HOME 变量应指示 OpenJDK 的安装目录,而 PATH 变量应指向 JDK 的 bin 目录。

JAVA_HOME

In our case, JAVA_HOME will be C:\Program Files\Java\jdk-21.0.2 and PATH will be C:\Program Files\Java\jdk-21.0.2\bin

在我们的例子中,JAVA_HOME将是C:\Program Files\Java\jdk-21.0.2 PATH将是C:\Program Files\Java\jdk-21.0.2\bin

Finally, we can confirm the installation’s success by running the following command in the command prompt:

最后,我们可以在命令提示符下运行以下命令来确认安装是否成功:

> java -version

After running the command above, a similar output will be displayed in the command prompt:Version

运行上述命令后,命令提示符中将显示类似的输出:Version

3. Chocolatey Package Manager

3.巧克力软件包管理器

Chocolatey is a popular package manager for Windows that simplifies the installation and management of software packages. It provides a command-line interface (CLI) that allows users to search for, install, and uninstall software packages with ease, similar to package managers like apt on Ubuntu or Homebrew on macOS.

Chocolatey是一款流行的 Windows 软件包管理器,可简化软件包的安装和管理。它提供了一个命令行界面(CLI),允许用户轻松搜索、安装和卸载软件包,类似于 Ubuntu 上的apt或 macOS 上的Homebrew等软件包管理器。

We need to first install Chocolatey on our machine before proceeding. Let’s open an elevated command and run the following command:

在继续之前,我们需要先在机器上安装 Chocolatey。让我们打开高架命令,运行以下命令:

> Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Once Chocolatey is installed, we can move forward with installing OpenJDK using it. Running the below command will install Java:

安装好 Chocolatey 后,我们就可以继续使用它安装 OpenJDK 了。运行以下命令即可安装 Java:

> choco install openjdk

4. Scoop Package Manager

4.勺子软件包管理器

Similar to Chocolatey, Scoop is another package manager designed specifically for Windows. Scoop is geared towards individual users rather than system-wide installations. It installs packages in the user’s home directory, which doesn’t require administrative privileges.

Scoop 与 Chocolatey 类似,是另一款专为 Windows 设计的软件包管理器。Scoop面向个人用户,而非全系统安装。它将软件包安装在用户的主目录中,无需管理员权限。

To get started with Scoop, we must first install it:

要开始使用 Scoop,我们必须先安装它:

> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser 
> Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression

Now, to install OpenJDK using Scoop, we need to open PowerShell as an administrator and execute the following commands:

现在,要使用 Scoop 安装 OpenJDK,我们需要以管理员身份打开 PowerShell 并执行以下命令:

> scoop bucket add java
> scoop install openjdk

5. Using Third-Party Installers

5.使用第三方安装程序

Some third-party tools and utilities offer simplified installation procedures for OpenJDK on Windows. For instance, tools like SDKMAN! and WinGet provide an easy-to-use interface for managing software installations, including OpenJDK.

一些第三方工具和实用程序为 Windows 上的 OpenJDK 提供了简化的安装程序。例如,SDKMAN!和WinGet等工具为管理软件安装(包括 OpenJDK)提供了一个易于使用的界面。

We can explore these options if we prefer a more streamlined installation process with additional features and customization options.

如果我们希望安装过程更加简化,并提供更多的功能和定制选项,我们可以探索这些选项。

6. Conclusion

6.结论

In this article, we explored different ways to install OpenJDK on our Windows machine. We can either opt for manual installation, package managers like Chocolatey or Scoop, or third-party installers, each method offers its advantages in simplicity, customization, and automation.

在本文中,我们探讨了在 Windows 机器上安装 OpenJDK 的不同方法。我们既可以选择手动安装,也可以选择 Chocolatey 或 Scoop 等软件包管理器,还可以选择第三方安装程序。

We can choose the approach that best fits our preferences and workflow.

我们可以选择最适合自己喜好和工作流程的方法。