Writing and Running Your First Program in Java
The classic first Java program prints "Hello, World!", but the interesting part is what's structurally required to get there. A source file must be named exactly after the public class it contains β HelloWorld.java for a class named HelloWorld, case included β because the compiler enforces that match. Inside, the JVM's launcher looks for one specific method signature, public static void main(String[] args), as the program's starting point.
Getting there takes two commands: javac to compile, then java to run the result. Since Java 11, a newer shortcut collapses both into one β java HelloWorld.java compiles and runs a single file directly without leaving a .class file behind β which is convenient for a first run, even though real projects still compile explicitly as a separate step.
Write the source file
Name it exactly HelloWorld.java, matching the public class name inside β the Java compiler enforces filename-to-class-name parity, capitalization included.
Compile it
javac HelloWorld.java reads the source, checks it for compile-time errors, and emits HelloWorld.class β a JVM bytecode file, not a native executable.
Run it
java HelloWorld (no .class extension here) starts a JVM, loads that bytecode, and looks specifically for a public static void main(String[] args) method to call.
Execution happens top to bottom
The JVM runs the body of main one statement at a time, in order, until the method returns or the program is told to stop.
A newer shortcut exists
Since Java 11, java HelloWorld.java alone compiles and runs a single-file program in one step, skipping the separate .class file β handy for quick tests, though real projects still compile explicitly through a build tool.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); }}Traditional: Compile, Then Run
Compiles to a .class file first, then runs that bytecode separately β the standard two-step approach used by real projects and build tools.
javac HelloWorld.javajava HelloWorldSingle-File Source Launch (Java 11+)
Skips the separate .class file entirely, compiling and running in memory in one command β handy for quick scripts and learning, not typically used for real projects.
java HelloWorld.javaRecommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself β building beats watching.