"Error: Main method not found in class MyClass, please define the main method as..."

New Java programmers often encounter messages like the following when they attempt to run a Java program. (Different Java tools, IDEs and so on give a variety of diagnostics for this problem.)


Error: Main method not found in class MyClass, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Error: Main method not found in the file, please define the main method as: 
   public static void main(String[] args)

Error: Main method is not static in class MyClass, please define the main method as:
   public static void main(String[] args)

Error: Main method must return a value of type void in class MyClass, please
define the main method as:
   public static void main(String[] args)

java.lang.NoSuchMethodError: main
Exception in thread "main"

What does this mean, what can cause it, and what should one do to fix it?


答案 1

When you use the command to run a Java application from the command line, e.g.,java

java some.AppName arg1 arg2 ...

the command loads the class that you nominated and then looks for the entry point method called . More specifically, it is looking for a method that is declared as follows:main

package some;
public class AppName {
    ...
    public static void main(final String[] args) {
        // body of main method follows
        ...
    }
}

The specific requirements for the entry point method are:

  1. The method must be in the nominated class.
  2. The name of the method must be "main" with exactly that capitalization1.
  3. The method must be .public
  4. The method must be 2.static
  5. The method's return type must be .void
  6. The method must have exactly one argument and argument's type must be 3.String[]

(The argument may be declared using syntax; e.g. . See this question for more information. The argument is used to pass the arguments from the command line, and is required even if your application takes no command-line arguments.)varargsString... argsString[]

If anyone of the above requirements is not satisfied, the command will fail with some variant of the message:java

Error: Main method not found in class MyClass, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Or, if you are running an extremely old version of Java:

java.lang.NoSuchMethodError: main
Exception in thread "main"

If you encounter this error, check that you have a method and that it satisfies all of the six requirements listed above.main


1 - One really obscure variation of this is when one or more of the characters in "main" is NOT a LATIN-1 character … but a Unicode character that looks like the corresponding LATIN-1 character when displayed.
2 - Here is an explanation of why the method is required to be static.
3 - String must be the standard java.lang.String class and not to a custom class named String that is hiding the standard class.


答案 2

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

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.


推荐