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

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

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


Error: Main met         


        
8条回答
  •  -上瘾入骨i
    2020-11-22 01:38

    Generally, it means the program you are trying to run does not have a "main" method. If you are going to execute a Java program, the class being executed must have a main method:

    For example, in the file Foo.java

    public class Foo {
        public static void main(final String args[]) {
            System.out.println("hello");
        }
    }
    

    This program should compile and run no problem - if main was called something else, or was not static, it would generate the error you experienced.

    Every executable program, regardless of language, needs an entry point, to tell the interpreter, operating system or machine where to start execution. In Java's case, this is the static method main, which is passed the parameter args[] containing the command line arguments. This method is equivalent to int main(int argc, char** argv) in C language.

提交回复
热议问题