Skip to main content
Java beginner Lesson 2 of 58

Setting Up Java

Install the JDK, set up IntelliJ IDEA, write your first Java program, and understand how Java code compiles and runs.

Before writing Java, you need two things: a JDK to compile and run code, and an IDE to write it comfortably. Getting this setup right once means you’ll never fight your environment again — every project you create will just work.

Install the JDK

The JDK (Java Development Kit) includes the compiler (javac), the JVM (java), and the standard library. Without it, you cannot compile or run Java programs. Always install a Long-Term Support (LTS) release — these are maintained for years and are safe for both learning and production work.

Download: adoptium.net — choose Temurin 21 (LTS), the latest long-term support release.

Windows

  1. Download the .msi installer for Windows x64
  2. Run it — the installer sets JAVA_HOME and adds java/javac to your PATH automatically
  3. Open a new terminal and verify:
java -version
# openjdk version "21.0.3" 2024-04-16
javac -version
# javac 21.0.3

macOS

# Using Homebrew (recommended)
brew install --cask temurin@21

java -version   # verify

Linux (Debian/Ubuntu)

sudo apt update
sudo apt install temurin-21-jdk   # after adding the Adoptium repo
java -version

Set Up IntelliJ IDEA

A good IDE removes friction from the whole development cycle — it catches errors before you run, suggests completions, and lets you navigate large codebases instantly. IntelliJ IDEA Community Edition is free and the most widely used Java IDE in the industry, so learning it now maps directly to professional workflows.

  1. Download from jetbrains.com/idea — choose Community Edition
  2. Install and launch it
  3. On the welcome screen: New Project
  4. Choose Java, select your JDK (21), click Next
  5. Give your project a name — HelloJava — and click Create

IntelliJ detects the JDK you installed automatically. If it doesn’t, click Add JDK and point it to your installation directory.

Your First Java Program

Writing and running a “Hello, World!” program is the fastest way to confirm your entire toolchain works. If this runs, your JDK installation, IDE configuration, and project structure are all correct.

In IntelliJ: right-click srcNewJava Class → name it Hello.

// File: Hello.java
public class Hello {

    public static void main(String[] args) {
        // This is the entry point — Java starts execution here
        System.out.println("Hello, World!");
    }
}

Click the green Run button (or press Shift+F10). You’ll see:

Hello, World!

How Compilation Works

Java is a two-step language: your source code is first compiled to platform-neutral bytecode, then the JVM executes that bytecode. This is what enables “Write Once, Run Anywhere” — the same .class file runs on Windows, macOS, and Linux without modification, as long as a JVM is installed.

Hello.java          (your source code — human-readable)

    │   javac Hello.java

Hello.class         (bytecode — platform-neutral binary)

    │   java Hello

"Hello, World!"     (JVM interprets bytecode on your OS)

You can do this manually from the terminal too:

# Navigate to the folder containing Hello.java
javac Hello.java        # creates Hello.class
java Hello              # runs it — prints: Hello, World!

The .class file runs identically on Windows, macOS, and Linux — as long as a JVM is installed.

Anatomy of the Hello World Program

Every line in a Java program has a specific purpose. Understanding what each piece does — even in this minimal example — builds the mental model you’ll use to read and write every Java program you encounter.

public class Hello {                    // class name must match filename
    public static void main(String[] args) {  // entry point — Java starts here
        System.out.println("Hello!");   // print to console + newline
    }
}
  • public class Hello — every Java file contains a class; the class name must exactly match the filename (Hello.java)
  • public static void main(String[] args) — the signature Java looks for to start execution; every standalone program needs exactly this
  • System.out.println(...) — prints to standard output with a newline; System.out.print(...) prints without a newline
  • Statements end with ;
  • Code blocks are wrapped in {}

IntelliJ Shortcuts Worth Learning Now

Learning a handful of keyboard shortcuts pays back immediately — you’ll spend less time navigating and more time thinking. These are the ones you’ll use every single day.

ActionWindows/LinuxmacOS
Run programShift+F10⌃R
Run current fileCtrl+Shift+F10⌃⇧R
Auto-completeCtrl+Space⌃Space
Quick fixAlt+Enter⌥Enter
Reformat codeCtrl+Alt+L⌘⌥L
Find in fileCtrl+F⌘F
Search everywhereShift+ShiftShift+Shift

Common First-Time Errors

These errors trip up almost every beginner. Knowing what causes them turns a confusing red message into a quick fix.

”class Hello is public, should be declared in a file named Hello.java”

The class name and filename must match exactly — including capitalisation.

“‘javac’ is not recognised as an internal or external command”

The JDK isn’t on your PATH. On Windows: re-run the installer, or add C:\Program Files\Eclipse Adoptium\jdk-21\bin to your System PATH manually.

”Main method not found”

The main method signature must be exactly public static void main(String[] args). A common mistake is Public (capital P) or missing static.

Project: Hello Java CLI App

Build a small program that greets the user and shows some basic information. This practices using variables, string formatting, and System.out — three tools you’ll use in every Java program.

public class HelloApp {

    public static void main(String[] args) {
        // Basic output
        System.out.println("=== Hello Java App ===");
        System.out.println("Java version: " + System.getProperty("java.version"));
        System.out.println("OS: " + System.getProperty("os.name"));

        // Simple calculation
        int year = 2024;
        int birthYear = 2000;
        int age = year - birthYear;
        System.out.println("If born in " + birthYear + ", you are " + age + " years old in " + year);

        // String formatting — printf-style, cleaner than concatenation for complex output
        String name = "Java Developer";
        System.out.printf("Welcome, %s! Happy coding.%n", name);
    }
}

Output:

=== Hello Java App ===
Java version: 21.0.3
OS: Windows 11
If born in 2000, you are 24 years old in 2024
Welcome, Java Developer! Happy coding.

Next up: Variables and Data Types — how Java stores and represents data.

Frequently Asked Questions

Which JDK version should I install?
Install a Long-Term Support (LTS) release — Java 21 is the latest LTS as of 2024. LTS versions receive security patches for years, making them the safe choice for learning and production alike.
Do I need IntelliJ IDEA? Can I use VS Code?
You can use any editor, but IntelliJ IDEA (Community Edition — free) is the industry standard for Java. It provides the best autocomplete, refactoring, and debugging experience out of the box.
What is the difference between javac and java?
javac is the compiler — it turns your .java source file into a .class bytecode file. java is the JVM launcher — it runs the bytecode. You compile once with javac, then run as many times as you want with java.