Install the JDK and Your First Program

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 (Hellohello).


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

  1. Class name ≠ filename. public class Hello requires Hello.java.
  2. Running java Hello.class. Use java Hello only.
  3. Wrong directory. javac and java must find the file/class on the classpath (default: current directory).
  4. Missing semicolon after println(...).
  5. Old JDK on PATH. java -version shows 21 but javac is 17—align both via JAVA_HOME.

Practice checkpoint

  1. Write a program SumArgs that prints the sum of two integer arguments.

    • Answer sketch: Parse args[0] and args[1] with Integer.parseInt, add, print.
  2. What file appears after javac Foo.java?

    • Answer: Foo.class
  3. Fix the error: file Main.java contains public class main { ... }

    • Answer: Rename class to Main or rename file to main.java (convention: use Main).
  4. What does public static void main(String[] args) mean in one phrase each for public, static, void?

    • Answer: public — JVM can call it; static — no instance needed; void — returns nothing.

Interview angles

  • “What is the signature of main?”public static void main(String[] args). Variants with String... args are 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 via java ClassName.

Next: /java/learn/getting-started/project-layout/