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
- JDK 21 installed (Windows / macOS / Linux)
- IntelliJ IDEA Community installed
- Basic familiarity with files and folders
- Terminal Hello World completed (recommended)
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
- Open IntelliJ IDEA
- Click New Project on the welcome screen (or File → New → Project)
- In the left sidebar, select Java
- 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
- Name:
- 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
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)| Item | Purpose |
|---|---|
src | Source code root—put .java files here |
out | Compiler output (bytecode); IntelliJ manages this |
.idea | Project 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
- In the Project tool window, right-click
src - New → Java Class
- Enter name
HelloWorld(IntelliJ adds.java) - Replace the generated stub with:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}- Save the file:
- Windows / Linux:
Ctrl + S - macOS:
Cmd + S
- Windows / Linux:
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:
| OS | Shortcut (default keymap) |
|---|---|
| Windows / Linux | Shift + F10 |
| macOS | Control + R |
Expected output in the Run tool window at the bottom:
Hello, World!
Process finished with exit code 0Step 5: What IntelliJ Did for You
When you clicked Run, IntelliJ roughly performed:
- Compiled
HelloWorld.javawith the project JDK 21 - Wrote bytecode under
out/(or equivalent output path) - Started the JVM with the correct classpath and main class
HelloWorld - Streamed
stdoutto 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:
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.
- Open the run configuration dropdown (top toolbar, near the green ▶)
- Choose Edit Configurations…
- Inspect Application → HelloWorld:
- Main class:
HelloWorld - Module: your project module
- JRE: 21
- Main class:
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
| Action | Windows / Linux | macOS |
|---|---|---|
| Run | Shift + F10 | Control + R |
| Debug | Shift + F9 | Control + D |
| Save all | Ctrl + S | Cmd + S |
| Find class | Ctrl + N | Cmd + O |
| Search everywhere | Double Shift | Double Shift |
| Format code | Ctrl + Alt + L | Cmd + 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
mainmethod - Fix: save the file; ensure
public static void main(String[] args)exists; use gutter ▶ next tomain
Error: Cannot find or load main class HelloWorld
- Likely cause: class outside
src, wrong package, or compile failed - Fix: put
HelloWorld.javaundersrc; 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
javaon 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:
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.