“错误:在 MyClass 类中找不到 Main 方法,请将 main 方法定义为...”
新的 Java 程序员在尝试运行 Java 程序时经常会遇到类似以下的消息。 (不同的 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"
这是什么意思,什么可能导致它,以及应该采取什么措施来解决它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
例如,当您使用
java
命令从命令行运行 Java 应用程序时,该命令会加载您指定的类,然后查找名为
main
的入口点方法。更具体地说,它正在寻找声明如下的方法:入口点方法的具体要求是:
public
。静态
2。void
。String[]
3。(参数可以使用
varargs
语法声明;例如String...args
。请参阅此问题了解更多信息。String[]
参数用于从命令行传递参数,即使您的应用程序采用没有命令行参数。)如果任何人如果不满足上述要求,
java
命令将失败,并显示以下消息的某些变体:或者,如果您运行的是极其旧版本的 Java:
如果您遇到此情况错误,请检查您是否有一个
main
方法,并且它满足上面列出的所有六个要求。1 - 一个非常模糊的变体是“main”中的一个或多个字符不是 LATIN-1 字符……而是一个 Unicode 字符,看起来相应的 LATIN-1显示时的字符。
2 - 这里解释了为什么该方法需要是静态的。
3 -
String
必须是标准java.lang.String
类,而不是隐藏标准类的名为String
的自定义类。< /sup>When you use the
java
command to run a Java application from the command line, e.g.,the command loads the class that you nominated and then looks for the entry point method called
main
. More specifically, it is looking for a method that is declared as follows:The specific requirements for the entry point method are:
public
.static
2.void
.String[]
3.(The argument may be declared using
varargs
syntax; e.g.String... args
. See this question for more information. TheString[]
argument is used to pass the arguments from the command line, and is required even if your application takes no command-line arguments.)If anyone of the above requirements is not satisfied, the
java
command will fail with some variant of the message:Or, if you are running an extremely old version of Java:
If you encounter this error, check that you have a
main
method and that it satisfies all of the six requirements listed above.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 standardjava.lang.String
class and not to a custom class namedString
that is hiding the standard class.问题是您尝试调用的类中没有
public void main(String[] args)
方法。它
static
请注意,您实际上已经指定了一个现有的类(否则错误会有所不同),但该类缺少 main 方法。
The problem is that you do not have a
public void main(String[] args)
method in the class you attempt to invoke.It
static
Note, that you HAVE actually specified an existing class (otherwise the error would have been different), but that class lacks the main method.
其他答案很好地总结了
main
的要求。我想收集记录这些要求的参考资料。最权威的来源是
VM
规范(引用的第二版)。由于main
不是一种语言特性,因此 Java 语言规范中没有考虑它。另一个很好的资源是应用程序启动器本身:
Other answers are doing a good job of summarizing the requirements of
main
. I want to gather references to where those requirements are documented.The most authoritative source is the
VM
spec (second edition cited). Asmain
is not a language feature, it is not considered in the Java Language Specification.Another good resource is the documentation for the application launcher itself:
如果您正在运行正确的类并且 main 已正确定义,还要检查您是否在同一包中定义了一个名为 String 的类。将考虑 String 类的此定义,并且由于它不符合
main(java.lang.String[] args)
,因此您将得到相同的异常。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.异常的名称表明程序尝试调用不存在的方法。在这种情况下,听起来该程序没有
main
方法,尽管如果您发布导致错误的代码以及运行该代码的上下文,这会有所帮助。如果用户尝试运行没有
main
方法的.class
文件或.jar
文件,则可能会发生这种情况 - 在 Java 中,main
方法是开始执行程序的入口点。通常编译器应该防止这种情况发生,所以如果这种情况确实发生,通常是因为被调用的方法的名称是在运行时而不是编译时确定的。
要解决此问题,新程序员必须添加中间方法(假设仍缺少
main
)或将方法调用更改为执行此操作的方法的名称存在。在这里阅读有关 main 方法的更多信息: http://csis.pace.edu/ 〜bergin/KarelJava2ed/ch2/javamain.html
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 nomain
method - in Java, themain
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
一般来说,这意味着您尝试运行的程序没有“main”方法。如果要执行 Java 程序,则正在执行的类必须有一个
main
方法:例如,在文件 Foo.java 中,
该程序应该编译和运行没有问题 - 如果
main
被称为其他名称,或者不是静态的,它会生成您遇到的错误。每个可执行程序,无论何种语言,都需要一个入口点来告诉解释器、操作系统或机器从哪里开始执行。在 Java 中,这是静态方法
main
,它传递包含命令行参数的参数args[]
。该方法相当于C语言中的int main(int argc, char** argv)
。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
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 parameterargs[]
containing the command line arguments. This method is equivalent toint main(int argc, char** argv)
in C language.我觉得上面的答案错过了即使您的代码有 main() 也会发生此错误的情况。当您使用 JNI 时,它使用 Reflection 来调用方法。在运行时如果找不到该方法,您将收到
java.lang.NoSuchMethodError: No virtual method
I feel the above answers miss a scenario where this error occurs even when your code has a main(). When you are using JNI that uses Reflection to invoke a method. During runtime if the method is not found, you will get a
java.lang.NoSuchMethodError: No virtual method
在我的例子中,“main”方法(程序的入口点)必须位于名为 java 文件的公共类中。
有关示例,请参阅以下案例。
这里,我将 main 方法放置在 HelloWorld 类中,该类的名称与 java 文件名相同。我创建了一个 Main 类的对象,并且代码运行正确。
但是,如果我将“main”方法放在 Main 类中,因为它不是 java 文件代码中的公共类,则会出现错误,
“在类 HelloWorld 中找不到 Main 方法,请将 main 方法定义为:
公共静态无效主(字符串[]参数)
或者JavaFX应用程序类必须扩展javafx.application.Application”
因此main方法必须放置在java文件的公共类中。
In my case "main" method which is the entry point of the programs must be located in the public class which is named as the java file.
For an example refer the below case.
Here, I've placed my main method inside the HelloWorld class which is named same as the java file name. And I created an object of Main class and the code runs correctly.
But if I placed "main" method inside the Main class, as it is not the public class in the java file code gives me the error ,
"Main method not found in class HelloWorld, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
Therefore main method must be placed in the public class of the java file.
如果您使用的是 VSCode:
选择:清理工作区
选择:重新启动并删除
继续编码:-)
If you are using VSCode:
Choose: Clean Workspace
Choose: Restart and delete
Keep coding :-)
对于那些在 Kotlin 项目中遇到此问题的人。
您可以尝试删除 .idea 文件夹并再次运行该项目 - 对我来说它解决了问题。 (最好先关闭 IntelliJ)
似乎有时 IntelliJ 对 main 方法签名感到困惑,并期望 java 签名而不是 Kotlin 签名。
For those who encountered this problem in Kotlin Project.
You can try deleting .idea folder and running the project again - for me it solved the issue. (better close IntelliJ first)
Seems like sometimes IntelliJ gets all confused about the main method signature and expecting the java signature instead of the Kotlin one.
几分钟后,我面临“主方法未定义”。现在它已解决。我尝试了上述所有方法,但没有任何效果。我的 java 文件中没有编译错误。
我按照下面的步骤
现在问题解决了。得到所需的结果。
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.
I followed below things
Now problem solved.Getting required result.