Learning goals
Install JDK 21, verify your setup from the terminal, compile and run a minimal program, and understand what happens at each step so you can debug “it doesn’t run” issues confidently.
Install JDK 21
Pick one approach:
| Platform | Recommended |
|---|---|
| macOS | Homebrew: brew install openjdk@21 |
| Windows | Eclipse Temurin or Oracle JDK installer |
| Linux | Package manager or Temurin tarball |
After installation, confirm:
java -version
javac -version
You should see version 21 (or 21.x.x). If javac is not found, the JDK—not just a JRE—is missing from your PATH.
JAVA_HOME (optional but useful) points to the JDK root. Build tools (Maven, Gradle) and IDEs read it.
# macOS example (Apple Silicon, Homebrew)
export JAVA_HOME=/opt/homebrew/opt/openjdk@21
export PATH="$JAVA_HOME/bin:$PATH"
Your first program
Create a folder, then a file named Hello.java. The public class name must match the filename.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Compile
javac Hello.java
This produces Hello.class (bytecode). Errors here are compile-time—syntax, type errors, missing semicolons.
Run
java Hello
Note: run with the class name, not Hello.class or Hello.java.
Output:
Hello, Java!
Anatomy of the program
public class Hello { // 1. class declaration
public static void main(String[] args) { // 2. entry point
System.out.println("Hello, Java!"); // 3. statement
}
}
| Piece | Role |
|---|---|
public class Hello |
Defines a type named Hello; filename must be Hello.java |
public static void main(String[] args) |
JVM entry point; args holds command-line arguments |
System.out.println |
Prints a line to standard output |
Curly braces delimit blocks. Semicolons end statements. Java is case-sensitive (Hello ≠ hello).
Command-line arguments
public class Greet {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Greet <name>");
return;
}
System.out.println("Hello, " + args[0] + "!");
}
}
javac Greet.java
java Greet Ada
# Hello, Ada!
args is a String array—we cover arrays later; for now treat it as a list of strings from the shell.
Using an IDE (optional)
IntelliJ IDEA Community, VS Code with Extension Pack for Java, or Eclipse all work. IDEs hide javac/java behind a Run button but under the hood they compile and invoke the same tools. Learning the terminal first makes IDE errors easier to interpret.
Common mistakes
- Class name ≠ filename.
public class HellorequiresHello.java. - Running
java Hello.class. Usejava Helloonly. - Wrong directory.
javacandjavamust find the file/class on the classpath (default: current directory). - Missing semicolon after
println(...). - Old JDK on PATH.
java -versionshows 21 butjavacis 17—align both viaJAVA_HOME.
Practice checkpoint
-
Write a program
SumArgsthat prints the sum of two integer arguments.- Answer sketch: Parse
args[0]andargs[1]withInteger.parseInt, add, print.
- Answer sketch: Parse
-
What file appears after
javac Foo.java?- Answer:
Foo.class
- Answer:
-
Fix the error: file
Main.javacontainspublic class main { ... }- Answer: Rename class to
Mainor rename file tomain.java(convention: useMain).
- Answer: Rename class to
-
What does
public static void main(String[] args)mean in one phrase each forpublic,static,void?- Answer:
public— JVM can call it;static— no instance needed;void— returns nothing.
- Answer:
Interview angles
- “What is the signature of main?” —
public static void main(String[] args). Variants withString... argsare allowed; other signatures won’t start the app. - “Compile-time vs runtime error?” — Syntax/type errors fail at
javac; wrong logic, bad input, or missing classes fail when running. - “Can you have multiple main methods?” — Multiple classes can define
main; you choose which to run viajava ClassName.