In this tutorial, you will learn how to write a simple Java program that prints “Hello World” to the console. You will also learn what each line means, how to compile the source file with javac, how to run the compiled class with java, and how to fix common beginner errors.

Java Hello World Program Overview

A Java Hello World program is usually the first program written by a beginner because it uses the smallest useful structure of a Java application: a class, a main() method, and a print statement. The program confirms that Java is installed correctly and that you can compile and run a Java source file from your computer.

Before we begin, ensure that your environment is set up to compile and run Java programs. If Java is not already installed, please follow the instructions to Install Java on your computer.

Writing HelloWorld.java Source File

Open your preferred text editor, such as Notepad on Windows, TextEdit in plain text mode on macOS, Gedit on Ubuntu, or any Java IDE. Type the following code and save the file as HelloWorld.java. In this example, the file name must match the public class name HelloWorld.

HelloWorld.java

</>
Copy
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Line-by-Line Meaning of the Java Hello World Code

Java codeMeaning
public class HelloWorldDefines a public class named HelloWorld. Because it is public, the source file should be saved as HelloWorld.java.
public static void main(String[] args)Defines the main method. The Java Virtual Machine starts program execution from this method.
System.out.println("Hello World");Prints the text Hello World and then moves the cursor to the next line.
{ }Curly braces group the body of the class and the body of the method.

Java is case-sensitive. For example, System and system are not the same. The spelling and capitalization in the program should be typed carefully.

Compiling HelloWorld.java with javac

Compilation is the process in which the Java compiler converts your source code into bytecode. This bytecode is then executed by the Java Runtime Environment (JRE), making Java programs platform independent.

To compile the program, navigate to the directory where HelloWorld.java is saved and run the following command:

tutorialkart@java:~$ javac HelloWorld.java

If the compilation completes without errors, a .class file, in this case HelloWorld.class, will be generated in the same folder.

tutorialkart@java:~$ ls
HelloWorld.class    HelloWorld.java

What HelloWorld.class Contains

The HelloWorld.class file does not contain ordinary source code. It contains Java bytecode, which is the compiled form of the program. You normally do not edit the .class file manually. If you want to change the program, edit HelloWorld.java and compile it again.

Running HelloWorld.class with java

Running the compiled .class file loads the bytecode into the Java Runtime Environment (JRE), which executes the program instructions. To run your program, use the following command from the directory containing the .class file:

tutorialkart@java:~$ java HelloWorld
Hello World

If the program executes correctly, you will see Hello World printed on the console. Notice that the run command uses java HelloWorld, not java HelloWorld.class. The class name is provided without the .class extension.

Common Java Hello World Errors and Fixes

Most errors in a first Java program are caused by file naming, capitalization, missing braces, or running the command from the wrong directory. The following checks help you identify the issue quickly.

Error or symptomLikely reasonFix
class HelloWorld is public, should be declared in a file named HelloWorld.javaThe file name does not match the public class name.Save the file exactly as HelloWorld.java.
javac: command not foundThe JDK is not installed or the PATH is not configured.Install the JDK and make sure the javac command is available from the terminal.
Error: Could not find or load main class HelloWorldYou may be in the wrong folder, or the class name in the run command is incorrect.Run the command from the folder containing HelloWorld.class and type java HelloWorld.
';' expectedA semicolon is missing after the print statement.End the statement as System.out.println("Hello World");.
Nothing prints after running the programThe print statement may be removed, commented, or placed outside the main method.Keep the print statement inside main().

Rules Used in This Java Hello World Program

The Java Hello World example introduces a few rules that you will use in almost every Java program:

  • The public class name and source file name should match exactly: HelloWorld and HelloWorld.java.
  • Java program execution starts from the main() method when you run a normal application.
  • Java statements such as System.out.println("Hello World"); end with a semicolon.
  • Java is case-sensitive, so String, System, and HelloWorld must be written with the correct capitalization.

Java Hello World QA Checklist

  • Confirm that the class name is HelloWorld and the file name is HelloWorld.java.
  • Confirm that the main() method is written as public static void main(String[] args).
  • Confirm that the print statement uses System.out.println with the correct capitalization.
  • Confirm that javac HelloWorld.java creates HelloWorld.class.
  • Confirm that the program is run with java HelloWorld from the same directory.

Java Hello World Program FAQs

What is Hello World in Java?

Hello World in Java is a simple program that prints Hello World to the console. It is commonly used to learn the basic structure of a Java application and to verify that Java is installed and working correctly.

Why should the Java file be named HelloWorld.java?

The file is named HelloWorld.java because the program contains a public class named HelloWorld. In Java, a public top-level class should be stored in a file with the same name as the class, followed by the .java extension.

Why do we use public static void main in Java Hello World?

The public static void main(String[] args) method is the entry point of a standard Java application. When you run java HelloWorld, the Java Virtual Machine looks for this method and starts execution from there.

Should I run java HelloWorld or java HelloWorld.class?

You should run java HelloWorld. Do not include the .class extension in the run command. The java command expects the class name, not the file name.

Is Java Hello World harder than C++ Hello World?

Java Hello World may look slightly longer because it requires a class and a main() method. C++ has a different program structure. The difficulty depends on what you are learning next, but for this first program, the important task is to understand the fixed Java structure and run it correctly.

What You Completed in the Java Hello World Program

You created HelloWorld.java, compiled it using javac, generated HelloWorld.class, and ran the program using the java command. This gives you the basic workflow used for simple Java programs: write source code, compile it, and run the compiled class.