“Error: Main method not found in class MyClass, please define the main method as…”

前端 未结 8 1306
南旧
南旧 2020-11-22 01:15

New Java programmers often encounter these messages when they attempt to run a Java program.


Error: Main met         


        
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 01:30

    The name of the exception suggests that the program tried to call a method that doesn't exist. In this context, it sounds like the program does not have a main method, though it would help if you posted the code that caused the error and the context in which the code was run.

    This might have happened if the user tried to run a .class file or a .jar file that has no main method - in Java, the main method is the entry point to begin executing the program.

    Normally the compiler is supposed to prevent this from happening so if this does happen, it's usually because the name of the method being called is getting determined ar run-time, rather than compile-time.

    To fix this problem, a new programmer must either add the midding method (assuming still that it's main that's missing) or change the method call to the name of a method that does exist.

    Read more about the main method here: http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

提交回复
热议问题