Card 05/ 06

GotchaDifficulty: Intermediate2 min

The main Method Signature the Launcher Looks For

It compiled without a word, and then the launcher said Main method not found.

The java launcher starts a program by calling one method with an exact signature: public static void main(String[] args). On Java 21 and earlier, a class without that exact method compiles cleanly and then fails when you run it, because the check belongs to the launcher, not the compiler.

The compiler accepted the file without a word. Running it gives this instead of your output:

text
javac Report.java

java Report
Error: Main method not found in class Report, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

The file:

Report.javajava
public class Report {
    public static void main() {
        System.out.println("Report");
    }
}
line 2 is a perfectly valid method, and it is not the one being looked for

main() with no parameters is a legal method. It has a name, a return type and a body, so there is nothing for the compiler to object to. The compiler's job is to check that what you wrote is valid Java, and it is.

The launcher is doing something different. When you hand it a class name it looks for one exact signature: public, static, returning void, called main, taking an array of String. Anything else is a method it will happily run if something calls it, and nothing is going to call it.

Copy the signature from the error message. It prints the exact shape it wants, and the fix is always to make your line match that line character for character.

← → move through sections and cards