Hello World in Java is the smallest complete Java program: it prints one line of text. Writing it teaches you the structure every Java program follows — a class, a main method and statements — and how to compile and run code. Every test automation framework you'll build with Selenium or REST Assured follows exactly the same structure.
- How to write, compile and run a Java Hello World program
- What every word in
public static void main(String[] args)means - The structure of a Java program: package, imports, class, fields, methods and main
- The four errors beginners hit most — with the exact messages and fixes
Java Hello World Program
Save this in a file named HelloWorld.java — the file name must match the class name exactly, including capital letters:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!
How to Compile and Run It
Open a terminal in the folder with the file. You need a JDK installed (Java 17 or 21 is a good choice). Then:
javac HelloWorld.java # compiles to HelloWorld.class
java HelloWorld # runs the class (no .class at the end)
Since Java 11 you can also skip the separate compile step for a single file:
java HelloWorld.java
Output
Hello, World!
No JDK yet? Try it straight away in the browser with the free Java online compiler.
Hello World Explained Line by Line
| Code | What it means |
|---|---|
public class HelloWorld | Declares a class named HelloWorld. public means any code can use it. Every Java program needs at least one class. |
public static void main(String[] args) | The entry point — the JVM starts running here. public: the JVM can call it. static: it runs without creating an object. void: it returns nothing. String[] args: command-line arguments. |
System.out.println("Hello, World!"); | Prints the text and moves to a new line. System is a class, out is the standard output stream, println is the method. |
; and { } | Every statement ends with a semicolon; curly braces mark where a class or method starts and ends. |
Structure of a Java Program
Real programs have more parts than Hello World. This example shows them in the order they appear in a file:
package demo; // 1. package (optional)
import java.time.LocalDate; // 2. imports
/** 3. class: the file is named after the public class */
public class Structure {
static final String SITE = "navtutorial"; // 4. fields (variables of the class)
// 5. methods
static String greet(String name) {
return "Hello, " + name + "!";
}
// 6. main method: where the program starts
public static void main(String[] args) {
String message = greet("Tester"); // 7. statements end with ;
System.out.println(message);
System.out.println("Learning Java on " + SITE + " in " + LocalDate.of(2026, 10, 1).getYear());
}
}
Output
Hello, Tester!
Learning Java on navtutorial in 2026
| # | Part | Required? | Purpose |
|---|---|---|---|
| 1 | package | Optional (one per file, must be first) | Groups related classes, like folders |
| 2 | import | Optional | Uses classes from other packages, e.g. java.time.LocalDate |
| 3 | class | Required | Holds fields and methods; a public class must match the file name |
| 4 | Fields | Optional | Data that belongs to the class |
| 5 | Methods | Optional | Reusable blocks of code |
| 6 | main method | Required to run a program | Where execution starts |
| 7 | Statements | — | Instructions, each ending with ; |
Compile and run a class that has a package by naming the package too: javac -d out Structure.java then java -cp out demo.Structure.
Common Mistakes
- File name doesn't match the class name (for example the class is HelloWorld but the file is Hello.java):
Fix: rename the file to HelloWorld.java.Hello.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java - Missing semicolon:
Fix: addHelloWorld.java:3: error: ';' expected System.out.println("Hello, World!") ^;where the caret^points. - Forgetting
staticon main — it compiles, but fails when you run it:Error: Main method is not static in class HelloWorld, please define the main method as: public static void main(String[] args) - Lowercase
system— Java is case-sensitive:
Fix: writeHelloWorld.java:3: error: package system does not existSystemwith a capital S.
Practice Exercises
- Change the program to print your name on a second line.
- Use
System.out.print(without "ln") twice and see how the output changes. - Add a method
static int add(int a, int b)and printadd(2, 3)from main.
Key Takeaways
- A runnable Java program needs a class and a
public static void main(String[] args)method. - The public class name and the file name must match exactly.
- Compile with
javac, run withjava— or run a single file directly withjava HelloWorld.java.
📚 Official documentation: dev.java: Getting started with Java · Java 21: the java launcher
Frequently Asked Questions
How do I run a Java Hello World program?
Save it as HelloWorld.java, run javac HelloWorld.java to compile, then java HelloWorld. From Java 11 you can run java HelloWorld.java directly.
Why is main static in Java?
Because the JVM calls main before any object exists. A static method belongs to the class, so it can run without creating an object.
What is the structure of a Java program?
Optional package statement, optional imports, then a class containing fields and methods, including the main method where execution starts. Statements end with semicolons.
Does the file name have to match the class name?
Yes, for a public class. If the class is public class HelloWorld, the file must be HelloWorld.java, with the same capital letters.
What is the difference between print and println?
println moves to a new line after printing; print stays on the same line.