Module 1: Getting Started with Java & Computing
1.3 JDK 21 Setup, Modern Tooling & Running Programs
Estimated reading & practice time: ~25 minutes
Learning Objectives
• Verify JDK 21 installation using command-line tools.
• Configure JAVA_HOME and system PATH.
• Run single-file Java programs without explicit compilation using 'java App.java'.
Mental Model & Analogy
Think of installing the JDK like setting up a professional chef's kitchen. The pantry contains all ingredients (Java Class Library), the stove is the compiler (javac), and the dining room is the JVM where prepared meals are served. Setting JAVA_HOME is like putting a signpost on the road so any food delivery driver knows where the kitchen is located.
Core Concept Breakdown
To develop Java applications locally, you need the Java Development Kit (JDK). Starting with Java 11 and enhanced in Java 21 LTS, Java includes a single-file source-code launcher (JEP 330). This means you can type `java Program.java` and Java will compile in-memory and execute the program in one step, without creating a visible `.class` file.
Essential Environment Variables:
• **`JAVA_HOME`**: Points to the root directory where the JDK is installed (e.g., `C:\Program Files\Java\jdk-21`).
• **`PATH`**: Tells your operating system where to find `javac` and `java` executables from any terminal directory.
Architecture Diagram
Code Example
JAVA Example
public class ToolingCheck {
public static void main(String[] args) {
long memory = Runtime.getRuntime().totalMemory() / (1024 * 1024);
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("=== JDK 21 Environment Check ===");
System.out.println("Java Vendor: " + System.getProperty("java.vendor"));
System.out.println("Available CPU Cores: " + cores);
System.out.println("Initial JVM Memory: " + memory + " MB");
}
}Line-by-Line Walkthrough
• `Runtime.getRuntime()` provides direct programmatic access to the JVM operating environment.
• `.availableProcessors()` queries the underlying CPU hardware cores available to Java.
• `.totalMemory()` returns the heap memory pool allocated to the process.
=== JDK 21 Environment Check === Java Vendor: Eclipse Adoptium Available CPU Cores: 8 Initial JVM Memory: 256 MB
Common Pitfalls & Debugging
• Installing only the JRE instead of the full JDK. The JRE lacks `javac`.
• Forgetting to restart your terminal after updating `PATH` or `JAVA_HOME`.
Try It Yourself — Java Sandbox
Live ExecutionTask: Run the `ToolingCheck` class inside the Code Sandbox or terminal and verify that your system returns the available CPU cores.
Solution.java
Edit the code and click Run Code to verify output directly inside TBLearn.
1
Terminal Output
Click "Run Code" or press Ctrl+Enter to execute.