A Java Hello World program is the smallest Java program that prints Hello, World! to the console. In Java 25, it is a single void main() method in a file, and on older versions it is a class with a public static void main(String[] args) method.
We write a Hello World program right after installing a JDK. It proves that the compiler and the JVM work and that the terminal finds them, before we spend time on real code.
The following example is the complete Java 25 version, saved as HelloWorld.java.
void main() {
IO.println("Hello, World!"); // Hello, World!
}
Notice that there is no class, no static and no String[] args. We run the Java 25 version first, write the classic version for older JDKs, compile and run it from the terminal, go through it line by line and fix the errors that beginners see most often.
1. Hello World in Java 25
Java 25 made compact source files and instance main methods a standard feature. A compact source file contains methods and fields without a class declaration around them. The compiler wraps them in a class named after the file, so HelloWorld.java becomes a class HelloWorld.
The method IO.println() belongs to the new class java.lang.IO. Classes in java.lang need no import, so IO works in every Java 25 program, not only in compact source files. We run the file with the java command and no compile step.
java HelloWorld.java
Hello, World!
A compact source file can also use every public class of the java.base module, such as List or LocalDate, without import statements. On Java 21 to 24, the same idea existed only as a preview feature behind the –enable-preview flag, so this version does not compile there without the flag. The IO class itself arrived in Java 23, in the java.io package, and moved to java.lang in Java 25.
2. The Classic Hello World Program
The classic version runs on every Java version, including Java 25, and it is what we see in most existing code bases and books. It declares a class and a main() method with a fixed signature.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
A public top-level class must be saved in a file with the same name, so the class HelloWorld goes into HelloWorld.java. Java is case-sensitive, so helloworld.java or HelloWorld.Java does not work. A class without public may live in a file with any name, but matching names help us find each class in a project.
3. Compiling and Running Hello World
Running a Java program from source takes two commands. The compiler javac reads the source file and writes a .class file with bytecode. The launcher java starts a JVM, loads that class and calls its main() method.

Before compiling, we check that both tools are on the PATH and report the same version. If javac is missing, the machine has only a runtime, or the JDK bin folder is not on the PATH. The guide to install OpenJDK covers the setup on each operating system.
java -version
javac -version
openjdk version "25.0.4.1" 2026-08-18 LTS
javac 25.0.4.1
We open a terminal in the folder that contains HelloWorld.java, compile it, and run the class by its name.
javac HelloWorld.java
java HelloWorld
Hello, World!
The compiler prints nothing when it succeeds and leaves HelloWorld.class next to the source file. The java command takes the class name, not the file name, so java HelloWorld.class fails with a ClassNotFoundException, as the errors in section 5 show.
Since Java 11, the launcher also accepts a source file. The command java HelloWorld.java compiles the file in memory and runs it without writing a .class file. Since Java 22, a program launched this way may also use other source files in the same folder, so small multi-class programs run without a build tool.
4. Hello World Line by Line
Each word in the classic version has a job, and the same keywords come back in every Java program we write later. The table maps each part of the program to what it does.
| Code | What it does |
|---|---|
| public class HelloWorld | Declares a class named HelloWorld that other classes can use. All code in the classic version lives inside a class. |
| public static void main(String[] args) | Declares the entry point. The JVM calls this method first. The Java main() method article explains each modifier and the Java 25 rules. |
| static | Lets the JVM call main() without creating a HelloWorld object first. |
| void | The method returns no value. A program reports its exit status with System.exit() instead. |
| String[] args | An array with the command-line arguments. It is empty when we pass none. |
| System.out.println(“Hello, World!”) | Prints the string and a line break to standard output. The System.out field is a PrintStream connected to the console. |
| { } and ; | Braces group the body of a class or method, and a semicolon ends each statement. |
The Java 25 version drops everything in that table except the call that prints the text. The compiler adds the class, and the launcher accepts a main() method that is neither public nor static and has no parameters. Once a program grows, we add a class declaration back without changing anything else.
5. Common Hello World Errors and Fixes
Almost every first program fails once, and the message points straight at the cause. The table lists the messages JDK 25 prints for the most common mistakes.
| Error message | Cause | Fix |
|---|---|---|
| ‘javac’ is not recognized … (Windows) or javac: command not found | No JDK installed, or its bin folder is not on the PATH | Install a JDK and add its bin folder to the PATH |
| class HelloWorld is public, should be declared in a file named HelloWorld.java | File name and public class name differ | Rename the file or the class so both match, including case |
| ‘;’ expected | A statement has no semicolon at the end | Add the semicolon at the position the caret shows |
| package system does not exist | system.out written in lowercase | Write System with a capital S |
| Could not find or load main class HelloWorld.class | The .class extension passed to java | Run java HelloWorld |
| Main method not found in class HelloWorld | Misspelled main, or a wrong return type such as int | Use void main() or the full classic signature |
The next source file has two of these mistakes, a lowercase system and a missing semicolon.
// does not compile: lowercase system and a missing semicolon
public class HelloWorld {
public static void main(String[] args) {
system.out.println("Hello, World!")
}
}
The compiler reports the syntax error first, with the line number and a caret under the position. After we add the semicolon, the next run reports the lowercase system.
HelloWorld.java:3: error: ';' expected
system.out.println("Hello, World!")
^
1 error
HelloWorld.java:3: error: package system does not exist
system.out.println("Hello, World!");
^
1 error
Running the compiled class by its file name fails at runtime instead, because the launcher looks for a class literally named HelloWorld.class.
Error: Could not find or load main class HelloWorld.class
Caused by: java.lang.ClassNotFoundException: HelloWorld.class
6. Running Hello World in an IDE
In an IDE, we create a class, type the main() method and press the green run icon next to it. IntelliJ IDEA, Eclipse and VS Code with the Java extensions all compile the class in the background and show the output in a console panel.
The terminal is still worth one run, for a practical reason. On a new laptop, a developer joins a team whose build uses Java 25, while an old Java 17 sits first on the PATH. Running java -version and the Hello World program from the terminal shows that mismatch in a minute, before Maven or Gradle stops with a less readable error about an unsupported Java release.
7. Printing More Than Hello World
Once Hello World runs, the next programs print values and read input. The println() method ends the line, print() keeps the cursor on the same line, and printf() fills placeholders such as %s and %d.
String name = "World";
String plain = "Hello, " + name + "!"; // "Hello, World!"
String formatted = String.format("Hello, %s!", name); // "Hello, World!"
int letters = name.length(); // 5
The printf-style formatting article lists all placeholders. To read a name from the keyboard, Java 25 offers IO.readln(), which prints a prompt and returns the typed line, or null when the input ends, so the safe version checks for both null and blank input.
public class Greeter {
public static void main(String[] args) {
String name = IO.readln("Your name: ");
if (name == null || name.isBlank()) {
name = "stranger";
}
IO.println("Hello, " + name.strip() + "!");
}
}
Your name: Lokesh
Hello, Lokesh!
8. Java Hello World Program FAQs
Readers who see Hello World print for the first time ask about the new signature, the file name and why the console window closes.
8.1. Do we still need public static void main in Java 25?
No. Since Java 25, void main() is enough, in a compact source file or in a regular class. The classic signature still works and is the only one that older JDKs accept, so library code and tutorials for Java 17 or 21 keep using it.
8.2. Can a Java program run without a class?
Only in appearance. A compact source file has no class declaration, but the compiler creates a final class named after the file. Inspecting the compiled HelloWorld.class with javap shows final class HelloWorld with a void main() method.
8.3. Does the file name have to match the class name?
Only for a public top-level class. A file can contain one public top-level class, and that class gives the file its name. Classes without public can live in a file with any name.
8.4. What is the difference between print and println?
The println() method prints the text and a line separator, whereas print() prints only the text. Two print() calls in a row put both texts on the same line.
8.5. Why does the program end right after printing?
Because main() has no more statements. The JVM exits when main() returns and no other non-daemon threads are running. In an IDE, the console stays open, while a double-clicked console window on Windows closes at once.
9. Conclusion
In Java 25, Hello World is one method, void main(), with an IO.println() call, and java HelloWorld.java runs it. The classic version with public class HelloWorld and public static void main(String[] args) works on every version and is what older code uses.
We compile with javac HelloWorld.java and run with java HelloWorld, without the .class extension. When something fails, the compiler message names the line and the cause, and the table of common errors covers the cases from a missing JDK to a misspelled main. The Java tutorial continues with variables, types and control flow.
10. References
- JEP 512, Compact Source Files and Instance Main Methods
- JEP 330, Launch Single-File Source-Code Programs
- JEP 458, Launch Multi-File Source-Code Programs
- java.lang.IO Javadoc
- The java launcher reference
Happy Learning !!
With the jdk21 version I had, “unnamed classes” are still a preview feature.
In order to build using preview features: