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
- Download the
.msiinstaller for Windows x64 - Run it — the installer sets
JAVA_HOMEand addsjava/javacto yourPATHautomatically - 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.
- Download from jetbrains.com/idea — choose Community Edition
- Install and launch it
- On the welcome screen: New Project
- Choose Java, select your JDK (21), click Next
- 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 src → New → Java 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 thisSystem.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.
| Action | Windows/Linux | macOS |
|---|---|---|
| Run program | Shift+F10 | ⌃R |
| Run current file | Ctrl+Shift+F10 | ⌃⇧R |
| Auto-complete | Ctrl+Space | ⌃Space |
| Quick fix | Alt+Enter | ⌥Enter |
| Reformat code | Ctrl+Alt+L | ⌘⌥L |
| Find in file | Ctrl+F | ⌘F |
| Search everywhere | Shift+Shift | Shift+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.