Java Tutorial Series: This lesson is part of our comprehensive Complete Java Tutorial: Learn Java from Beginner to Advanced.

Java Hello World Program: Write and Run Your First Java Program

The Hello World program is one of the simplest ways to verify that Java is installed and to understand the basic structure of a Java application.

Although the program is small, it introduces several concepts that become important later: classes, methods, the main method, strings, statements, source files, compilation, bytecode, and program execution.

This guide shows how to write Hello World, compile it with javac, run it with java, understand every part of the code, use an IDE, troubleshoot common errors, and extend the program into slightly more useful examples.

What You Will Learn

  • How to write your first Java program
  • How to save a Java source file
  • How to compile Java code
  • How to run a compiled Java class
  • What public class means
  • Why the file name matters
  • What main means in Java
  • How System.out.println works
  • How Java source code becomes executable bytecode
  • How to troubleshoot common Hello World errors

1. What Is the Java Hello World Program?

A Hello World program prints a simple message to the console. It is commonly used as a first program because it requires very little code while demonstrating the basic structure of a Java application.

When this program runs, the output is:

Hello, World!

2. Understanding the Java Program

public class Main

This declares a class named Main. Java code is organized into classes, and the class provides the structure that contains the program's methods and other members.

public static void main(String[] args)

This declares the main method, which is the standard entry point used when launching a Java application from the command line.

System.out.println(…)

This statement writes text to the standard output, normally the console, and then moves to a new line.

"Hello, World!"

This is a String literal. It represents the text that the program sends to the console.

Braces { }

The braces define blocks. The class body is one block, and the main method body is another nested block.

Semicolon ;

The println statement ends with a semicolon, which terminates the Java statement.

3. How to Create the Hello World File

Open a text editor or Java IDE and create a file named Main.java.

Enter:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Save the file as Main.java. The class is named Main, so the public class declaration and file name must match.

4. Compile the Java Program

Open a terminal in the directory containing Main.java.

javac Main.java

The javac command invokes the Java compiler. If compilation succeeds, Java creates a compiled class file, typically Main.class.

The simplified flow is:

Main.java → javac → Main.class

5. Run the Java Program

After compilation, run the class using the java command:

java Main

Do not normally write java Main.class when using the standard class-launching command. The argument identifies the class to launch.

The result is:

Hello, World!

6. Source Code, Bytecode, and the JVM

Hello World is useful because it lets you see the basic Java development pipeline.

You write Java source code in Main.java.

The javac compiler translates the source into Java bytecode.

The JVM loads and executes the bytecode.

The program produces output through the standard output stream.

This is part of the reason Java programs can be developed for different platforms while targeting the Java platform rather than compiling source directly into one operating system's native executable format.

7. Why Does the File Name Matter?

In this example, the class is declared as public class Main. A public top-level class must use a source file name that matches the class name, so the file is Main.java.

For example:

public class Hello

should be stored in:

Hello.java

A mismatch can cause a compilation error.

8. What Does static Mean in main?

The main method is declared static so the Java runtime can invoke it through the class without first creating an object of that class.

At this beginner stage, remember the practical idea: the runtime can start the program through the static main method before your program creates its own objects.

9. What Does String[] args Mean?

String[] args represents an array of strings that can receive command-line arguments.

For example, if you run:

java Main Rakshit

the value Rakshit can be received through args.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello " + args[0]);
    }
}

Output:

Hello Rakshit

10. Printing More Than One Line

You can call println multiple times.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        System.out.println("Welcome to Java.");
        System.out.println("Let us start coding.");
    }
}

Output:

Hello, World!
Welcome to Java.
Let us start coding.

11. print() vs println()

Java provides both print and println for console output.

System.out.print("Hello ");
System.out.print("Java");

Output:

Hello Java

println adds a line break after printing:

System.out.println("Hello");
System.out.println("Java");

Output:

Hello
Java

12. Printing Variables

Hello World can quickly be extended to introduce variables.

public class Main {
    public static void main(String[] args) {
        String name = "Rakshit";
        int age = 22;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

This example combines output with a String variable and an integer variable.

13. A Simple User Input Example

After Hello World, a natural next step is learning how a program receives input.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = scanner.nextLine();

        System.out.println("Hello " + name);
        scanner.close();
    }
}

This introduces imports, objects, input, and resource management. These topics should be studied more deeply later rather than memorized from the example.

14. Running Hello World in an IDE

You can also create and run the program inside a Java IDE.

  • Create a Java project.
  • Select a compatible JDK.
  • Create a Java class named Main.
  • Add the main method.
  • Write the println statement.
  • Run the class using the IDE's run action.
  • Read the output in the IDE's console.

An IDE hides some command-line details, but the underlying concepts remain the same: source code is compiled and the resulting program is launched on the Java platform.

15. Common Hello World Errors

'javac' is not recognized

The JDK compiler may not be installed or may not be available through PATH. Verify the JDK installation with javac -version.

Could not find or load main class

Check the current directory, the class name, compilation result, and the command used to launch the class. Make sure you are using the correct class name.

Class Main is public, should be declared in a file named Main.java

The public class name and source file name do not match. Rename the file to Main.java or change the public class name to match the file.

Missing main method

If you want to launch the class directly, make sure it contains a correctly declared main method.

';' expected

A Java statement may be missing its terminating semicolon. Check the line indicated by the compiler and the line immediately before it.

16. Hello World as Your First Java Debugging Exercise

Do not treat compiler errors as failures. Use them as your first debugging practice.

For example, if you write:

System.out.println("Hello World")

the compiler can report a syntax error because the statement is missing a semicolon.

System.out.println("Hello World");

Fixing small errors like this teaches you how Java's compiler communicates problems and how to read error messages.

17. Beginner Exercises After Hello World

Print your name.

Print your name, age, and city on separate lines.

Print a short introduction using variables.

Accept a name from the command line and print a greeting.

Print the multiplication table of a number.

Read a number and print whether it is positive or negative.

Create a simple calculator using user input.

These exercises gradually move you from output to variables, input, conditions, and basic programming logic.

18. What Should You Learn After Hello World?

Hello World should be the beginning of your programming practice, not the end.

A useful next sequence is:

Hello World → Java Syntax → Variables → Data Types → Operators → Input/Output → Conditions → Loops → Methods → Arrays and Strings → Collections → OOP

Once these foundations are comfortable, continue toward exception handling, file handling, modern Java features, multithreading, JVM concepts, frameworks, projects, and specialization.

19. Frequently Asked Questions

What is the first Java program beginners usually write?

A Hello World program that prints a message to the console.

Why is the Java file called Main.java?

Because the example declares a public class named Main, and the source file name must match the public top-level class name.

What does javac do?

javac compiles Java source code into Java bytecode.

What does java Main do?

It launches the Main class using the Java runtime.

Do I need an IDE for Hello World?

No. A text editor and the JDK command-line tools are enough.

Why do I need public static void main?

It defines the standard entry point that the Java runtime can use to start a conventional Java application.

What is Main.class?

It is the compiled bytecode output produced from Main.java by the Java compiler.

What should I learn after Hello World?

Move into syntax, variables, data types, operators, conditions, loops, methods, and data structures, followed by OOP and advanced Java.

Conclusion

The Java Hello World program is small, but it introduces the basic workflow of Java development: write source code, compile it, and run the resulting program.

Understanding what each part of the program does is more valuable than simply copying it. Once you can create, compile, run, and troubleshoot Hello World yourself, you are ready to move into Java syntax and core programming concepts.

Start small. Understand every line. Then build larger programs one concept at a time.

  • How to Install Java
  • How to Learn Java
  • Java Syntax
  • Java Variables
  • Java Data Types
  • Java Operators
  • Java Conditional Statements
  • Java Loops
  • Java Methods
  • Java Arrays
  • Java Strings
  • Java Classes and Objects