线程“main”中的异常 java.lang.NoSuchMethodError: main

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3540065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:41:11  来源:igfitidea点击:

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

javaexception

提问by Ramesh

Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main

可能的重复:
线程“main”中的异常 java.lang.NoSuchMethodError: main

I got the above message. The code is as follows:

我收到了上面的消息。代码如下:

class Test
{
 public static void main(String ar[])
 {
  printf("hai");
 }
}

How is this problem caused and how can I fix it?

这个问题是如何引起的,我该如何解决?

回答by NullUserException

In addition to the problem that's causing the current exception (see BalusC's answer), the proper "Hello World" in Java is:

除了导致当前异常的问题(参见 BalusC 的回答),Java 中正确的“Hello World”是:

class Test
{
    public static void main(String[] args) {
        System.out.println("hai");
    }
}

See: java.lang.System

看: java.lang.System

回答by LLS

I see your problem, the signature is not correct. It should be public static void main(String[] args)

我看到你的问题,签名不正确。它应该是 public static void main(String[] args)

回答by BalusC

The class which you're trying to execute doesn't have a mainmethod.

您尝试执行的类没有main方法。

Since your mainmethod looks syntactically fine, this can have two causes:

由于您的main方法在语法上看起来不错,这可能有两个原因:

  1. You're executing the wrong class.
  2. The actual class file doesn't contain this code.
  1. 你正在执行错误的类。
  2. 实际的类文件不包含此代码。

The solution is obvious:

解决方案很明显:

  1. Make sure that your command is pointing the correct class file, you might have multiple class files with the same name and be sitting in the wrong directory.
  2. Make sure that you've compiled the correct source file into the correct class file before, you might have edited one and other and forgot to recompile.
  1. 确保您的命令指向正确的类文件,您可能有多个同名的类文件并且位于错误的目录中。
  2. 确保您之前已经将正确的源文件编译成正确的类文件,您可能已经编辑了一个和另一个而忘记重新编译。

回答by Jay Askren

It could also be a class path issue which causes Eclipse to get confused and not able to find your class when it tries to run it. I would look at the Java Build Path in the Project Properties to make sure there are no errors.

这也可能是一个类路径问题,导致 Eclipse 感到困惑,并且在尝试运行它时无法找到您的类。我会查看项目属性中的 Java 构建路径以确保没有错误。