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

前端 未结 8 1035
南旧
南旧 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:52

    The problem is that you do not have a public void main(String[] args) method in the class you attempt to invoke.

    It

    • must be static
    • must have exactly one String array argument (which may be named anything)
    • must be spelled m-a-i-n in lowercase.

    Note, that you HAVE actually specified an existing class (otherwise the error would have been different), but that class lacks the main method.

    0 讨论(0)
  • 2020-11-22 01:54

    If you are running the correct class and the main is properly defined, also check if you have a class called String defined in the same package. This definition of String class will be considered and since it doesn't confirm to main(java.lang.String[] args), you will get the same exception.

    • It's not a compile time error since compiler just assumes you are defining a custom main method.

    Suggestion is to never hide library java classes in your package.

    0 讨论(0)
提交回复
热议问题