Java 开发环境配置

下载安装 JDK、配置 JAVA_HOME 与 PATH、验证安装并选择开发工具。

Beginner
Key Points装 JDK 后要设 JAVA_HOME 并把 %JAVA_HOME%\bin 加进 PATH;用 java -version 和 javac -version 验证,两者版本必须一致。

window 系统安装 java

Download a JDK from Oracle, or a distribution such as Adoptium Temurin, Amazon Corretto or Alibaba Dragonwell. Keep the install path free of spaces and non-ASCII characters, e.g. C:\Java\jdk-21 — it makes the environment setup much easier.

Text
安装目录示例:
C:\Java\jdk-21\
    bin\      javac.exe  java.exe  jar.exe
    lib\      核心类库
    conf\     配置文件

环境变量设置

Create a system variable JAVA_HOME pointing at the JDK root, then add %JAVA_HOME%\bin to Path. Switching JDK versions later means editing only JAVA_HOME.

Text
JAVA_HOME = C:\Java\jdk-21
Path      = %JAVA_HOME%\bin;%Path%

Linux / macOS(~/.bashrc 或 ~/.zshrc):
export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
You must open a new terminal after changing environment variables; an already-open one keeps the old environment.

验证安装

Run java -version and javac -version; the versions must match. A mismatch usually means another JRE sits earlier in PATH. echo %JAVA_HOME% confirms the variable.

Text
> java -version
java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode)

> javac -version
javac 21.0.2

流行 Java 开发工具

The IDE handles writing, debugging and refactoring; the build tool pulls dependencies and packages. For beginners, IDEA plus Maven is the smoothest: creating a project generates the Maven layout automatically.

ToolRole
IntelliJ IDEAIDE, free Community edition
EclipseIDE, plugin ecosystem
MavenDependency and build lifecycle
GradleBuild tool with a Groovy/Kotlin DSL
JUnit 5Unit testing

Friends & Partners