Week 4 · Objectives 第 4 周 · 学习目标 ← Back to Hub ← 返回主页

Introduction to the Java Environment Java 开发环境介绍

Learning Objectives 学习目标

  • Explain the compilation and execution process of a Java program.
  • Demonstrate compiling and running Java code using command-line tools.
  • Identify the roles of JDK, JRE, and JVM.
  • 解释 Java 程序的编译和执行过程。
  • 演示如何使用命令行工具编译和运行 Java 代码。
  • 识别 JDK、JRE 和 JVM 的作用。
Bilingual Navigation:双语导航: Read and toggle languages using the globe button at the top. Test your understanding at the end of this page. 阅读并使用顶部的语言按钮切换中英文。在页面底部进行自我检测。

2 · Java Platform Pillars (JDK, JRE, JVM)

Java's cross-platform portability ("Write Once, Run Anywhere") is enabled by three core layered technologies: Java 的跨平台移植性(“一次编写,到处运行”)是由三个核心分层技术实现的:

Component组件 Full Name全称 Description & Contents说明与包含内容
JVM Java Virtual Machine Executes Java Bytecode line-by-line. Translates it into host system machine instructions.逐行执行 Java 字节码。将其翻译为主机系统的机器指令。
JRE Java Runtime Environment Contains the JVM plus core runtime class libraries needed to run compiled Java files.包含 JVM 以及运行编译后的 Java 文件所需的关键运行时类库。
JDK Java Development Kit The complete developers package. Contains JRE, compiler (javac), and debugging toolkits.完整的开发包。包含 JRE、编译器 (javac) 和调试工具集。

3 · Compilation & Execution Flow

Java uses a hybrid execution model. High-level code is first compiled into intermediate Bytecode, which is then interpreted by the JVM: Java 采用混合执行模式。高级代码首先被编译为中间字节码,然后由 JVM 进行解释执行:

  1. Source Code (.java):源程序代码 (.java): The programmer writes human-readable Java source files.程序员编写人类可读的 Java 源文件。
  2. Compilation (javac):程序编译 (javac): The Java Compiler parses the source file and translates it into bytecode (stored as a .class file).Java 编译器解析源文件并将其翻译成字节码(存储为 .class 文件)。
  3. Bytecode (.class):Java 字节码 (.class): A machine-independent set of optimized bytecode instructions.一组与机器无关的优化字节码指令。
  4. JVM Translation:JVM 解释执行: The JVM loads the class file, interprets the bytecode, and outputs native machine code for execution on the host CPU.JVM 加载类文件,解释字节码,并输出本地机器码,在主机 CPU 上执行。

4 · Command Line Compilation & Execution

Understanding the CLI commands builds a mental model of compilation before hiding it behind IDE buttons. 在将这些编译过程隐藏在 IDE 按钮后面之前,理解命令行 (CLI) 指令可以建立编译的思维模型。

Step 1: Write Source File步骤 1:编写源文件

Create a text file named Welcome.java:创建一个名为 Welcome.java 的文本文件:

public class Welcome {{
    public static void main(String[] args) {{
        System.out.println("Welcome to Java Programming!");
    }}
}}

Step 2: Compile to Class File步骤 2:编译为 Class 文件

Run the Java Compiler in terminal:在终端运行 Java 编译器:

javac Welcome.java

Outcome: A new file named Welcome.class is created in the directory.结果:在目录下生成一个新的 Welcome.class 文件。

Step 3: Run class in JVM步骤 3:在 JVM 中运行类

Launch JVM execution in terminal (omit extension):在终端启动 JVM 执行(省略文件后缀名):

java Welcome

Output: Welcome to Java Programming!

5 · IDE Configuration & Environment

Modern development is conducted inside Integrated Development Environments (IDEs) like Eclipse, IntelliJ IDEA, or VS Code: 现代软件开发通常在集成开发环境 (IDE) 中进行,例如 Eclipse、IntelliJ IDEA 或 VS Code:

Eclipse / IntelliJ Setup

1. Download and install JDK 21.
2. Install Eclipse IDE.
3. Bind JDK path in Preferences -> Installed JREs.
1. 下载并安装 JDK 21。
2. 安装 Eclipse IDE。
3. 在 Preferences -> Installed JREs 中绑定 JDK 路径。

Environment Paths

Ensure JAVA_HOME system path points to the JDK home directory, and %JAVA_HOME%\bin is added to the Path variable.确保系统路径 JAVA_HOME 指向 JDK 主目录,并且 %JAVA_HOME%\bin 已添加至 Path 变量中。

Applied Case Study: Environment Validation应用案例研究:开发环境验证

Company: Kompas Tech Solutions.
Let's simulate compiling and running a system utility program that checks Java runtime details.
公司: Kompas Tech Solutions。
让我们模拟编译和运行一个检查 Java 运行时信息的系统工具程序。

Validation Source Code验证程序的源代码

public class SystemCheck {{
    public static void main(String[] args) {{
        System.out.println("Checking Kompas Tech Java Environment...");
        System.out.println("Java Version: " + System.getProperty("java.version"));
        System.out.println("Java Vendor: " + System.getProperty("java.vendor"));
    }}
}}
💡 Hint / 提示

Save this code as SystemCheck.java, compile it in terminal, and run it. Verify your Java installation is JDK 21.将此代码保存为 SystemCheck.java,在终端进行编译,然后运行。验证您的 Java 版本是否为 JDK 21。

✅ Show Expected Console Output / 显示预期控制台输出
Checking Kompas Tech Java Environment...
Java Version: 21.0.2
Java Vendor: Oracle Corporation

Check Your Understanding自我检测

Q1. Which component is responsible for executing Java Bytecode?哪个组件负责执行 Java 字节码?

Q2. What is the file extension of a compiled Java file?编译后的 Java 文件的扩展名是什么?

Q3. Which CLI command is used to compile a Java file named Test.java?使用哪个命令行指令来编译名为 Test.java 的 Java 文件?

Q4. What is the correct relationship between JDK, JRE, and JVM?JDK、JRE 和 JVM 之间正确的包含关系是什么?