Card 05/ 06
All 6 cards
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.
$ 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.ApplicationHere is what the file actually had in it.
public class Report {
public static void main() {
System.out.println("Report");
}
}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.