Card 05/ 06

GotchaDifficulty: Intermediate1 min

It Compiled, and Then Said No Main Method Found

The compiler accepted the file without a word. You run it and get 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

Here is what the file actually had in it.

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 Java Virtual Machine — the JVM — 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, which is more useful than it looks — the fix is always to make your line match that line character for character.