线程“main”中的异常 java.lang.NoSuchMethodError: main
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2496073/
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
Exception in thread "main" java.lang.NoSuchMethodError: main
提问by David
when i try to compile this:
当我尝试编译这个时:
public class Risk
{
}
class territory
{
public static void main (String[]arg)
{
System.out.println ("hi") ;
}
}
I get this error message:
我收到此错误消息:
Exception in thread "main" java.lang.NoSuchMethodError: main
whats going wrong here?
这里出了什么问题?
采纳答案by David
What the answer wound up being was that the class i run must contain mainor else it won't work. i'm posting this because, though other answers give roughly the same information, they don't make it explicit.
最终的答案是我运行的课程必须包含main,否则它将无法工作。我发布这个是因为,虽然其他答案提供了大致相同的信息,但他们并没有明确说明。
回答by Benjamin Cox
The class containing the main()function must be public, and you may only define one public class per file. You'll want to have two separate files Risk.javaand Territory.java.
包含该main()函数的类必须是公共的,并且每个文件只能定义一个公共类。您需要有两个单独的文件Risk.java和Territory.java.
Risk.java:
风险.java:
public class Risk {
}
Territory.java:
领土.java:
public class Territory
{
public static void main (String[]arg)
{
System.out.println ("hi") ;
}
}
EDIT: It turns out this isn't true - I was able to run your initial code with the following command line:
编辑:事实证明这不是真的 - 我能够使用以下命令行运行您的初始代码:
java territory
But my earlier comments point to the best practice for a real app, such as a Risk game.
但我之前的评论指出了真实应用程序的最佳实践,例如风险游戏。
回答by Chris Jester-Young
What class are you trying to run? If you're using the class territory, that will work. Riskhas no mainmethod, though.
你想跑什么课?如果您正在使用 class territory,那将起作用。Risk没有main办法,虽然。
回答by Amil
Can you figure out why this example causes the same issue?
你能弄清楚为什么这个例子会导致同样的问题吗?
public class Simple {
public void main(String args[]) {
System.out.println("Inside function");
}
}
Answer: because main() should be public staticvoid!
答:因为 main() 应该是 public staticvoid!
回答by elduff
Could it just be a spacing issue? Your original post shows no space between the ']' and 'arg'.
会不会只是间距问题?您的原始帖子显示 ']' 和 'arg' 之间没有空格。
Try this:
试试这个:
public static void main (String[] arg)
or, if that still doesn't work:
或者,如果这仍然不起作用:
public static void main (String arg[])

