Skip to main content
Java · lesson 2 of 66
Open the Java page →
Java beginner Lesson 2 of 66

Setting Up Java

Install the JDK, set up IntelliJ IDEA, write and run your first Java program, and understand how javac and the JVM turn source code into output.

By SkillByExample · Updated

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. On the Linux machine used for this course it prints:
$ java -version
openjdk version "21.0.12.1" 2026-08-18
OpenJDK Runtime Environment (build 21.0.12.1+1-1-24.04.4-Ubuntu)
OpenJDK 64-Bit Server VM (build 21.0.12.1+1-1-24.04.4-Ubuntu, mixed mode, sharing)
$ javac -version
javac 21.0.12.1

Your version string and vendor (Temurin, Ubuntu’s OpenJDK, Oracle…) will differ; what matters is that both commands answer and the major version is 21 or later. java -version writes to standard error, which is why some scripts redirect it with 2>&1.

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 “Hello, World!” confirms the whole toolchain works. In IntelliJ: right-click src → New → Java Class → name it Hello, then press the green Run button (Shift+F10). From a terminal it looks like this:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
$ javac Hello.java
$ ls
Hello.class
Hello.java
$ java Hello
Hello, World!

How Compilation Works

Java runs in two steps: javac compiles source to platform-neutral bytecode in a .class file, and the java launcher starts a JVM that loads and executes it. The same .class file runs on Windows, macOS and Linux.

Hello.java   --javac-->   Hello.class   --java-->   JVM runs it on your OS

You can look inside the bytecode with javap, which ships with the JDK:

$ javac Hello.java
$ javap -c Hello
Compiled from "Hello.java"
public class Hello {
  public Hello();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #13                 // String Hello, World!
       5: invokevirtual #15                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}

The compiler added a constructor you never wrote (public Hello()), and main became four JVM instructions: fetch System.out, load the string, call println, return. Since Java 11, java Hello.java also compiles and runs a single file in one step, in memory — the form used in the rest of this course.

Anatomy of the Hello World Program

public class Hello {                              // class name must match the file name
    public static void main(String[] args) {      // the entry point Java looks for
        System.out.println("Hello!");             // print a line to standard output
    }
}
  • public class Hello — a public class must live in a file with the same name, Hello.java
  • public static void main(String[] args) — where execution starts; args holds command-line arguments
  • System.out.println(...) prints with a newline; System.out.print(...) prints without one
  • statements end with ; and 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 trip up almost every beginner. Each one below is the real message from JDK 21.

File name doesn’t match the public class

public class Greeter {
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}
$ javac Hello.java
Hello.java:1: error: class Greeter is public, should be declared in a file named Greeter.java
public class Greeter {
       ^
1 error

Saved as Hello.java, the class must be called Hello — capitalisation included.

main is missing static

public class Hello {
    public void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
$ javac Hello.java
$ java Hello
Error: Main method is not static in class Hello, please define the main method as:
   public static void main(String[] args)

It compiles — main is a legal method name — but the launcher refuses to start it. The signature must be exactly public static void main(String[] args).

Running with the .class extension

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
$ javac Hello.java
$ java Hello.class
Error: Could not find or load main class Hello.class
Caused by: java.lang.ClassNotFoundException: Hello.class

java takes a class name (Hello), not a file name. Only .java source files may be passed by file name.

‘javac’ is not recognised / command not found

The JDK’s bin folder isn’t on your PATH. On Windows, re-run the installer with the “Set JAVA_HOME / add to PATH” options, or add C:\Program Files\Eclipse Adoptium\jdk-21...\bin to the system PATH and open a new terminal.

Project: Hello Java CLI App

A small program that greets the user and reports the environment it runs in.

import java.time.LocalDate;

public class HelloApp {
    public static void main(String[] args) {
        String name = args.length > 0 ? args[0] : "Java Developer";
        System.out.println("=== Hello Java App ===");
        System.out.println("Java version: " + System.getProperty("java.version"));
        System.out.println("OS: " + System.getProperty("os.name"));

        int birthYear = 2000;
        int year = LocalDate.now().getYear();
        System.out.println("Born in " + birthYear + ", you turn " + (year - birthYear) + " in " + year);
        System.out.printf("Welcome, %s! Happy coding.%n", name);
    }
}
$ java HelloApp.java Ada
=== Hello Java App ===
Java version: 21.0.12.1
OS: Linux
Born in 2000, you turn 26 in 2026
Welcome, Ada! Happy coding.

The version, operating system and year come from the machine and the clock, so your output will differ in those lines. System.getProperty reads JVM properties; java -XshowSettings:properties -version lists them all.

Practice

  1. Print your JDK’s version, vendor and home folder using System.getProperty.
Solution
public class JdkInfo {
    public static void main(String[] args) {
        for (String key : new String[] {"java.version", "java.vendor", "java.home"}) {
            System.out.println(key + " = " + System.getProperty(key));
        }
    }
}
$ java JdkInfo.java
java.version = 21.0.12.1
java.vendor = Ubuntu
java.home = /usr/lib/jvm/java-21-openjdk-amd64

These three lines differ per machine; java.home is the folder JAVA_HOME should point to.

  1. Change Hello to use print instead of println twice in a row. What does the output look like?
Solution
public class PrintVsPrintln {
    public static void main(String[] args) {
        System.out.print("Hello, ");
        System.out.print("World!");
        System.out.println();
        System.out.println("next line");
    }
}
$ java PrintVsPrintln.java
Hello, World!
next line

print adds no newline, so both pieces land on one line; the bare println() ends it.

  1. Compile Hello.java into a separate classes folder and run it from there.
Solution
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from the classes folder");
    }
}
$ javac -d classes Hello.java
$ java -cp classes Hello
Hello from the classes folder
$ java Hello
Error: Could not find or load main class Hello
Caused by: java.lang.ClassNotFoundException: Hello

-cp classes tells the JVM where to look; without it, java only searches the current folder, where no Hello.class exists.

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

Frequently Asked Questions

Which JDK version should I install?
Install a Long-Term Support (LTS) release such as Java 21 or Java 25. LTS versions receive security patches for years, making them the safe choice for learning and production alike. The examples on this site were run on JDK 21 and work unchanged on newer releases.
What does JAVA_HOME do?
JAVA_HOME is an environment variable pointing at the JDK's installation folder. Build tools such as Maven and Gradle, and many IDEs, read it to find the compiler. The java and javac commands themselves are found through PATH, so set both when installing a JDK by hand.
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.