Write and Run Hello World in IntelliJ IDEA

Introduction

This chapter walks you through creating a Java project in IntelliJ IDEA Community Edition, writing HelloWorld, and running it from the IDE. Compared with the terminal-only flow, IntelliJ adds syntax highlighting, compile errors inline, and one-click run—while still using your installed JDK 21 under the hood.

Prerequisites

Why Run Hello World in IntelliJ IDEA

Running Hello World in the IDE is not only about printing text. You learn a repeatable workflow: create project → set JDK → add class → run → read output.

Key benefits:

  • Clear project layout (src, output folders)
  • Run configurations you can reuse
  • Shortcuts that speed up daily coding
  • Errors shown before you hunt through terminal messages

Tip

Best Practice

Use one IntelliJ project per chapter or exercise until you are comfortable merging work. It keeps SDK settings and run configurations easy to find.

Step 1: Create a New Java Project

  1. Open IntelliJ IDEA
  2. Click New Project on the welcome screen (or File → New → Project)
  3. In the left sidebar, select Java
  4. Configure:
    • Name: java-chapter7-hello
    • Location: choose a path you can find (e.g. ~/Projects/java-chapter7-hello)
    • Build system: IntelliJ (simplest for now—no Maven/Gradle yet)
    • JDK: 21 (add JDK manually if the list is empty—see editor install)
    • Add sample code: leave unchecked so you write Hello World yourself
  5. Click Create

IntelliJ creates a project with a src source root. Production code for this tutorial lives under src until you use build tools in later chapters.

Step 2: Understand the Project Layout

text
java-chapter7-hello/
├── .idea/                 # IDE settings (can stay in project folder)
├── src/                   # Your .java source files go here
│   └── HelloWorld.java    # (you will create this)
└── out/                   # Compiled .class files (generated; do not edit)
ItemPurpose
srcSource code root—put .java files here
outCompiler output (bytecode); IntelliJ manages this
.ideaProject metadata; usually commit only if your team agrees

This differs from the terminal chapter where HelloWorld.java and HelloWorld.class sat in the same folder. IntelliJ separates source (src) and output (out) by convention.

Step 3: Create HelloWorld.java

  1. In the Project tool window, right-click src
  2. New → Java Class
  3. Enter name HelloWorld (IntelliJ adds .java)
  4. Replace the generated stub with:
java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  1. Save the file:
    • Windows / Linux: Ctrl + S
    • macOS: Cmd + S

Warning

The public class name must match the filename: HelloWorld in HelloWorld.java. This rule is the same as in the terminal chapter.

Step 4: Run the Program

Use any of these methods:

Method 1 (recommended): Click the green ▶ in the gutter next to main.

Method 2: Right-click inside the editor → Run 'HelloWorld.main()'.

Method 3: Menu Run → Run 'HelloWorld'.

Method 4 — keyboard shortcuts:

OSShortcut (default keymap)
Windows / LinuxShift + F10
macOSControl + R

Expected output in the Run tool window at the bottom:

text
Hello, World!
 
Process finished with exit code 0

Step 5: What IntelliJ Did for You

When you clicked Run, IntelliJ roughly performed:

  1. Compiled HelloWorld.java with the project JDK 21
  2. Wrote bytecode under out/ (or equivalent output path)
  3. Started the JVM with the correct classpath and main class HelloWorld
  4. Streamed stdout to the Run window

You did not type javac or java manually, but the same steps happened. To see that, open the Terminal tool window inside IntelliJ and run:

bash
cd src
javac HelloWorld.java
java HelloWorld

(From src, you may see .class next to the source; the IDE normally keeps output in out/ instead.)

Step 6: Run Configuration (Optional but Useful)

After the first run, IntelliJ creates a Run/Debug Configuration named HelloWorld.

  1. Open the run configuration dropdown (top toolbar, near the green ▶)
  2. Choose Edit Configurations…
  3. Inspect Application → HelloWorld:
    • Main class: HelloWorld
    • Module: your project module
    • JRE: 21

You rarely need to change this for simple programs, but knowing it exists helps when you add more classes later.

Useful Shortcuts to Learn Early

ActionWindows / LinuxmacOS
RunShift + F10Control + R
DebugShift + F9Control + D
Save allCtrl + SCmd + S
Find classCtrl + NCmd + O
Search everywhereDouble ShiftDouble Shift
Format codeCtrl + Alt + LCmd + Option + L

Shortcuts can vary slightly by keymap (Settings → Keymap). The table uses the default IntelliJ keymap.

Common Problems and Fixes

Nothing happens when clicking Run

  • Likely cause: file not saved, or no main method
  • Fix: save the file; ensure public static void main(String[] args) exists; use gutter ▶ next to main

Error: Cannot find or load main class HelloWorld

  • Likely cause: class outside src, wrong package, or compile failed
  • Fix: put HelloWorld.java under src; fix red underlines; Build → Rebuild Project

Red underline on System or String

  • Likely cause: project SDK not set to JDK 21
  • Fix: File → Project Structure → Project → SDK → select 21

Output shows a different Java version than expected

  • Likely cause: project SDK ≠ system java on PATH
  • Fix: align Project SDK with the JDK from your install chapter; verify Run configuration uses the same JRE

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

  • Likely cause: typo in filename or class name
  • Fix: rename class or file so they match exactly, including capitalization

Mini Practice

Change the message and run again:

java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from IntelliJ Chapter 7!");
    }
}

If the Run window shows your new text, your IntelliJ Java workflow is ready.

What’s Next

Next, learn coding standards that apply to every Java file you write: Java Coding Standards. Then you will move into variables, operators, and core syntax.

FAQ

Should I use IntelliJ or the terminal to run beginner code?

Use both. The terminal teaches compile-run fundamentals; IntelliJ improves speed and readability. They complement each other.

Why put code in src instead of the project root?

Standard layout keeps sources separate from build output and prepares you for Maven/Gradle structures later.

Do I need Maven or Gradle for Hello World?

No. IntelliJ as the build system is enough for early chapters.

Can I rename the project folder after creation?

Yes, but prefer renaming through the IDE or update paths carefully so the project still opens correctly.

Is Community Edition enough for Spring Boot or Android later?

Community Edition covers Java language learning. Ultimate adds frameworks such as Spring and Jakarta EE in one product; you can also add those tools later when needed.