Lesson 2 — Environment

Java Setup

A correctly configured Java development environment lets you write, compile, and run programs efficiently. Getting this right now saves hours of frustration later.

What You Need to Install

To write and compile Java you need the JDK — Java Development Kit. The JDK contains:

  • javac — the Java compiler that turns .java source into .class bytecode.
  • java — the JVM launcher that executes your compiled bytecode.
  • The standard library — thousands of ready-made classes for collections, file IO, networking, and more.
  • Developer toolsjavadoc, jar, jshell, and others.

Do not install the JRE (Java Runtime Environment) alone — that only lets you run programs, not compile them. You need the full JDK.

Which JDK Version?

Download JDK 21 (the current Long-Term Support release). LTS versions receive security and bug fixes for years, making them the safe choice for learning and production alike. Download from Adoptium Temurin (free, open source, widely used in industry) at adoptium.net.

Verifying Your Installation

After installing, open a terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run:

JAVA — Verify installation
java -version
// Expected output (version number may differ):
// openjdk version "21.0.2" 2024-01-16
// OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13)

javac -version
// Expected output:
// javac 21.0.2

If either command says "not recognised" on Windows, the JDK was not added to your PATH. Reinstall and tick Add to PATH, or set the environment variable manually in System Properties → Environment Variables → PATH.

Choosing an IDE

An IDE (Integrated Development Environment) is a smart editor that understands Java — it shows errors as you type, autocompletes class and method names, and runs your program with one click. Your IT Tutor recommends:

  • IntelliJ IDEA Community Edition (free) — the industry standard for Java. Used at virtually every major Java employer. Download from jetbrains.com/idea.
  • VS Code with the Extension Pack for Java (free) — lighter weight, excellent for beginners.

Either choice is fine for this course. The skills transfer fully.

Compiling and Running from the Terminal

Understanding the terminal workflow is essential even when you use an IDE — you will encounter it in build systems, servers, and job interviews.

JAVA — Compile and run
// Step 1: Create Hello.java with your text editor

// Step 2: Compile it (run this in the same folder as Hello.java)
javac Hello.java
// This creates Hello.class in the same directory

// Step 3: Run the compiled bytecode
java Hello
// Output: Sanibonani, world!

// NOTE: you run "java Hello" (the class name), NOT "java Hello.class"

Project Structure

For this course, keep things simple: one folder per project, one .java file per class. As you advance, you will learn about packages and build tools like Maven and Gradle that organise larger projects.

JAVA — Simple project layout
MyFirstProject/
    Hello.java        ← source file
    Hello.class       ← compiled bytecode (created by javac)

Using IntelliJ IDEA

In IntelliJ: File → New Project → Java → select your JDK → create. Right-click Hello.java in the file tree and choose Run 'Hello.main()'. The IDE compiles and runs in one step, showing output in the console at the bottom.

Practice Task

Your Turn

Install JDK 21 from Adoptium. Verify both java -version and javac -version print the correct version. Create Hello.java, compile it from the terminal, and run it. Then open the same file in your IDE and run it from there.

Common Mistakes

  • Installing JRE instead of JDK — you cannot compile with just the JRE.
  • Running java Hello.class instead of java Hello — omit the .class extension.
  • Compiling from the wrong directory — the terminal must be in the same folder as your .java file.
  • Mixing JDK versions — use one consistent version across your machine.
  • On Windows: JDK not added to PATH after installation — reinstall and tick the PATH option.

Professional Tip

Always verify your toolchain works before starting a project. A five-minute environment check saves hours of mysterious errors.

Mini Quiz

What command compiles a Java source file?