Skip to main content

How to Write the main Method of a Java Program

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every Java program starts somewhere: the main method. When you run a program, Java looks for main in the class you named and runs it, passing in the words you typed after the program's name.

Syntax​

Syntax
public class <ClassName> {
public static void main(String[] args) {
<the program's first statements>
}
}
PartWhat it does
public static void mainThe method Java calls first. static means it runs without creating an object first.
String[] argsThe command-line arguments, as an array of strings.
<ClassName>The class that holds main; its file is usually named after it, such as Main.java.

The Java Language Specification, section 12.1.4, describes how Java picks the method: one with "a single formal parameter whose declared type is an array of String, a void result", or, since Java SE 25, one with "no formal parameters". It adds that "The form of a candidate main method expanded significantly in Java SE 25."

The Example​

The example prints a greeting and the number of arguments it received, once without arguments and once with two.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Class​

Create a file named Main.java:

Main.java, a public class Main whose public static void main(String[] args) prints Hello from main and the number of arguments.

Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello from main");
System.out.println("Arguments: " + args.length);
}
}

Step 2: Run It With and Without Arguments​

Terminal
java Main.java
java Main.java one two
Output
Hello from main
Arguments: 0
Hello from main
Arguments: 2

A zsh terminal where java Main.java prints Hello from main and Arguments: 0, and java Main.java one two prints Hello from main and Arguments: 2.

java Main.java compiled the file in memory and ran main. The words after the file name became args, so the second run counted two. The commands are the same in zsh, bash and PowerShell.

The Shorter Form in Java 25​

Java 25 also accepts a much shorter program. A file with no class around its main is a compact source file:

Hello.java
void main() {
IO.println("Hello from a compact source file");
}

Run with java Hello.java, it printed Hello from a compact source file. JEP 512 introduced this form so that "beginners can write their first programs without needing to understand language features designed for large programs." The classic form above still works everywhere and is what most existing code uses.

Common Mistakes​

MistakeWhat happensDo this instead
Misspelling main, or giving it other parameterserror: can't find main(String[]) or main() method in class: MainName it exactly main, with String[] args or no parameters
Expecting args[0] when no argument was givenjava.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0Check args.length first
Using a compact source file on an older JDKJDK 17 stopped with error: class, interface, enum, or record expectedUse the classic form there, or JDK 25

Troubleshooting​

SymptomLikely causeFix
can't find main(String[]) or main() methodNo method named main with an accepted formAdd public static void main(String[] args)
java Main.java isn't recognizedNo JDK on the PATHInstall a JDK and check with java -version

FAQ​

Does main have to be static?​

Not any more: since Java 25, Java can create an object of the class and call an instance main. The static form is still the common one.

Do I need javac first?​

Not for a single file: java Main.java compiles it in memory. Larger programs compile with javac and then run with java.

Conclusion​

public static void main(String[] args) is where a Java program starts, and args holds the command-line arguments. Java 25 also accepts a plain void main(), which keeps first programs short.

Additional Resources​