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.

🎯 What you'll learn
  • 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!
Advertisement

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

CodeWhat it means
public class HelloWorldDeclares 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
#PartRequired?Purpose
1packageOptional (one per file, must be first)Groups related classes, like folders
2importOptionalUses classes from other packages, e.g. java.time.LocalDate
3classRequiredHolds fields and methods; a public class must match the file name
4FieldsOptionalData that belongs to the class
5MethodsOptionalReusable blocks of code
6main methodRequired to run a programWhere execution starts
7Statements—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

  1. File name doesn't match the class name (for example the class is HelloWorld but the file is Hello.java):
    Hello.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
    Fix: rename the file to HelloWorld.java.
  2. Missing semicolon:
    HelloWorld.java:3: error: ';' expected
            System.out.println("Hello, World!")
                                               ^
    Fix: add ; where the caret ^ points.
  3. Forgetting static on 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)
  4. Lowercase system — Java is case-sensitive:
    HelloWorld.java:3: error: package system does not exist
    Fix: write System with a capital S.

Practice Exercises

  1. Change the program to print your name on a second line.
  2. Use System.out.print (without "ln") twice and see how the output changes.
  3. Add a method static int add(int a, int b) and print add(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 with java — or run a single file directly with java 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.