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

发布于 2024-10-25 22:42:34 字数 1116 浏览 5 评论 0原文

新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

半岛未凉 2024-11-01 22:42:34

例如,当您使用 java 命令从命令行运行 Java 应用程序时,

java some.AppName arg1 arg2 ...

该命令会加载您指定的类,然后查找名为 main 的入口点方法。更具体地说,它正在寻找声明如下的方法:

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

入口点方法的具体要求是:

  1. 该方法必须位于指定的类中。
  2. 方法的名称必须是“main”,完全大写1
  3. 该方法必须是public
  4. 该方法必须是静态2
  5. 该方法的返回类型必须为void
  6. 该方法必须只有一个参数,并且参数的类型必须为 String[] 3

(参数可以使用varargs语法声明;例如String...args。请参阅此问题了解更多信息。String[] 参数用于从命令行传递参数,即使您的应用程序采用没有命令行参数。)

如果任何人如果不满足上述要求,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

或者,如果您运行的是极其旧版本的 Java:

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

如果您遇到此情况错误,请检查您是否有一个 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.,

java some.AppName arg1 arg2 ...

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:

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 static 2.
  5. The method's return type must be void.
  6. The method must have exactly one argument and argument's type must be String[] 3.

(The argument may be declared using varargs syntax; e.g. 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.)

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

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 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 standard java.lang.String class and not to a custom class named String that is hiding the standard class.

羅雙樹 2024-11-01 22:42:34

问题是您尝试调用的类中没有 public void main(String[] args) 方法。

  • 必须是static
  • 必须恰好有一个字符串数组参数(可以命名为任何名称)
  • 必须以小写形式拼写 main。

请注意,您实际上已经指定了一个现有的类(否则错误会有所不同),但该类缺少 main 方法。

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.

转瞬即逝 2024-11-01 22:42:34

其他答案很好地总结了 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). As main 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:

假装爱人 2024-11-01 22:42:34

如果您正在运行正确的类并且 main 已正确定义,还要检查您是否在同一包中定义了一个名为 String 的类。将考虑 String 类的此定义,并且由于它不符合 main(java.lang.String[] args),因此您将得到相同的异常。

  • 这不是编译时错误,因为编译器只是假设您正在定义自定义 main 方法。

建议永远不要在包中隐藏库 java 类。

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.

柏拉图鍀咏恒 2024-11-01 22:42:34

异常的名称表明程序尝试调用不存在的方法。在这种情况下,听起来该程序没有 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 no main method - in Java, the main 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

烦人精 2024-11-01 22:42:34

一般来说,这意味着您尝试运行的程序没有“main”方法。如果要执行 Java 程序,则正在执行的类必须有一个 main 方法:

例如,在文件 Foo.java 中,

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

该程序应该编译和运行没有问题 - 如果 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

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.

奶气 2024-11-01 22:42:34

我觉得上面的答案错过了即使您的代码有 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

送舟行 2024-11-01 22:42:34

在我的例子中,“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.
enter image description here

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.

小伙你站住 2024-11-01 22:42:34

如果您使用的是 VSCode:

在此处输入图像描述

选择:清理工作区

在此处输入图像描述

选择:重新启动并删除

继续编码:-)

If you are using VSCode:

enter image description here

Choose: Clean Workspace

enter image description here

Choose: Restart and delete

Keep coding :-)

夜清冷一曲。 2024-11-01 22:42:34

对于那些在 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.

年少掌心 2024-11-01 22:42:34

几分钟后,我面临“主方法未定义”。现在它已解决。我尝试了上述所有方法,但没有任何效果。我的 java 文件中没有编译错误。
我按照下面的步骤

  • 简单地在所有依赖项目中进行了 Maven 更新(alt+F5)。

现在问题解决了。得到所需的结果。

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

  • simply did maven update in all dependent project (alt+F5).

Now problem solved.Getting required result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文