简体   繁体   English

“错误:在 MyClass 类中找不到主方法,请将主方法定义为...”

[英]"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.新的 Java 程序员在尝试运行 Java 程序时经常会遇到如下消息。 (Different Java tools, IDEs and so on give a variety of diagnostics for this problem.) (不同的 Java 工具、IDE 等针对此问题提供了多种诊断方法。)


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?这是什么意思,什么会导致它,应该怎么做才能解决它?

When you use the java command to run a Java application from the command line, eg,当您使用java命令从命令行运行 Java 应用程序时,例如,

java some.AppName arg1 arg2 ...

the command loads the class that you nominated and then looks for the entry point method called main .该命令加载您指定的类,然后查找名为main的入口点方法。 More specifically, it is looking for a method that is declared as follows:更具体地说,它正在寻找一个声明如下的方法:

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 capitalization 1 .方法的名称必须是“main”,并且完全大写1
  3. The method must be public .该方法必须是public
  4. The method must be static 2 .该方法必须是static2
  5. The method's return type must be void .方法的返回类型必须是void
  6. The method must have exactly one argument and argument's type must be String[] 3 .该方法必须只有一个参数,并且参数的类型必须是String[] 3

(The argument may be declared using varargs syntax; eg String... args . See this question for more information. The String[] argument is used to pass the arguments from the command line, and is required even if your application takes no command-line arguments.) 可以使用varargs语法声明该参数;例如String... args 。有关更多信息,请参阅此问题String[]参数用于从命令行传递参数,即使您的应用程序不使用命令也是必需的-line 参数。)

If anyone of the above requirements is not satisfied, the java 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 版本:

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

If you encounter this error, check that you have a main 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. 1 - 一个真正晦涩的变体是“main”中的一个或多个字符不是 LATIN-1 字符……而是一个 Unicode 字符,在显示时看起来像相应的 LATIN-1 字符。
2 - Here is an explanation of why the method is required to be static. 2 -这里解释了为什么该方法需要是静态的。
3 - String must be the standard java.lang.String class and not to a custom class named String that is hiding the standard class. 3 - String必须是标准的java.lang.String类,而不是隐藏标准类的名为String的自定义类。

The problem is that you do not have a public void main(String[] args) method in the class you attempt to invoke.问题是您尝试调用的类中没有public void main(String[] args)方法。

It

  • must be static必须是static
  • must have exactly one String array argument (which may be named anything)必须只有一个字符串数组参数(可以命名为任何名称)
  • must be spelled main in lowercase.必须以小写形式拼写 main。

Note, that you HAVE actually specified an existing class (otherwise the error would have been different), but that class lacks the main method.请注意,您实际上已经指定了一个现有的类(否则错误会有所不同),但该类缺少 main 方法。

Other answers are doing a good job of summarizing the requirements of main .其他答案在总结main的要求方面做得很好。 I want to gather references to where those requirements are documented.我想收集有关记录这些要求的参考资料。

The most authoritative source is the VM spec (second edition cited).最权威的来源是VM规范(引用的第二版)。 As main is not a language feature, it is not considered in the Java Language Specification.由于main不是语言特性,Java 语言规范中没有考虑它。

Another good resource is the documentation for the application launcher itself:另一个很好的资源是应用程序启动器本身的文档:

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.如果您正在运行正确的类并且 main 定义正确,还要检查您是否在同一个包中定义了一个名为 String 的类。 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.将考虑 String 类的这个定义,因为它没有与main(java.lang.String[] args)确认,你会得到同样的异常。

  • It's not a compile time error since compiler just assumes you are defining a custom main method.这不是编译时错误,因为编译器只是假设您正在定义自定义 main 方法。

Suggestion is to never hide library java classes in your package.建议永远不要在包中隐藏库 java 类。

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.在这种情况下,听起来程序没有main方法,但如果您发布导致错误的代码和运行代码的上下文会有所帮助。

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.如果用户尝试运行没有main方法的.class文件或.jar文件,则可能会发生这种情况 - 在 Java 中, main方法是开始执行程序的入口点。

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.要解决这个问题,新程序员必须要么添加中间方法(假设仍然缺少它的main方法) ,要么将方法调用更改为确实存在的方法的名称。

Read more about the main method here: http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html在此处阅读有关主要方法的更多信息:http: //csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

Generally, it means the program you are trying to run does not have a "main" method.通常,这意味着您尝试运行的程序没有“main”方法。 If you are going to execute a Java program, the class being executed must have a main method:如果你要执行一个 Java 程序,被执行的类必须有一个main方法:

For example, in the file Foo.java例如,在文件 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.这个程序应该可以编译和运行没有问题——如果main被调用了别的东西,或者不是静态的,它会产生你遇到的错误。

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.在 Java 的情况下,这是静态方法main ,它被传递给包含命令行参数的参数args[] This method is equivalent to int main(int argc, char** argv) in C language.该方法等价于 C 语言中的int main(int argc, char** argv)

I feel the above answers miss a scenario where this error occurs even when your code has a main().我觉得上面的答案错过了即使您的代码具有 main() 也会发生此错误的情况。 When you are using JNI that uses Reflection to invoke a method.当您使用使用反射调用方法的 JNI 时。 During runtime if the method is not found, you will get a在运行时如果没有找到方法,你会得到一个

java.lang.NoSuchMethodError: No virtual method

If you are using VSCode:如果您使用的是 VSCode:

在此处输入图像描述

Choose: Clean Workspace选择:清洁工作区

在此处输入图像描述

Choose: Restart and delete选择:重启并删除

Keep coding :-)继续编码:-)

For those who encountered this problem in Kotlin Project.对于那些在Kotlin 项目中遇到这个问题的人。

You can try deleting .idea folder and running the project again - for me it solved the issue.您可以尝试删除 .idea 文件夹并再次运行该项目 - 对我来说它解决了这个问题。 (better close IntelliJ first) (最好先关闭 IntelliJ)

Seems like sometimes IntelliJ gets all confused about the main method signature and expecting the java signature instead of the Kotlin one.似乎有时 IntelliJ 对主要方法签名感到困惑,并期待 java 签名而不是 Kotlin 签名。

Few min back i was facing " main method not defined".Now its resolved.I tried all above thing but nothing was working.There was not compilation error in my java file.几分钟后,我面临“未定义主要方法”。现在它已解决。我尝试了以上所有方法,但没有任何效果。我的 java 文件中没有编译错误。 I followed below things我跟着下面的事情

  • simply did maven update in all dependent project (alt+F5).只是在所有依赖项目中进行了 maven 更新(alt+F5)。

Now problem solved.Getting required result.现在问题解决了。得到所需的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在 class Main 中找不到 Main 方法,请定义 main 方法 - Error: Main method not found in class Main, please define the main method 错误:在类中找不到main方法,请定义main方法 - Error:main method not found in the class, please define the main method 错误:在 class XXX 中找不到主方法,请定义主方法 - Error: main method not found in class XXX, please define the main method 错误:在类mainGUI中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class mainGUI, please define the main method as: public static void main(String[] args) “错误:在Grad类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)” - “Error: Main method not found in class Grad, please define the main method as: public static void main(String[] args)” 错误:在 class 中找不到主要方法,请将主要方法定义为:public static void main(String[] args) - Error: Main method not found in class, please define the main method as: public static void main(String[] args) 错误:在类Text中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Text, please define the main method as: public static void main(String[] args) 错误:在Binary类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Binary, please define the main method as: public static void main(String[] args) 错误:在TextBook类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class TextBook, please define the main method as: public static void main(String[] args) 错误 - ''在 class 节点中找不到主方法,请将主方法定义为...”在以下代码中: - Error - ''Main method not found in class Node, please define the main method as…" in the following code:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM