Eclipse:如何设置项目的主要方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1747676/
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
Eclipse: How to set the main method for a project?
提问by cc.
In Eclipse, when you create a new class, you can select the option "Which methods stubs would like to create:" and you can choose " public static void main(String args[])".
在 Eclipse 中,当您创建一个新类时,您可以选择选项“存根想要创建的方法:”,并且您可以选择“ public static void main(String args[])”。
I have created a class with a main method, but I haven't checked that option and I receive this error: "selection does not contain a main type in eclipse". How can I show Eclipse that I have a main method in the class, without deleting/recreating the class file?
我创建了一个带有 main 方法的类,但我没有选中该选项,并且收到此错误:“选择不包含 Eclipse 中的主要类型”。如何在不删除/重新创建类文件的情况下向 Eclipse 显示我在类中有一个 main 方法?
Later Edit: :) My mistake: My main method looks like this: public static void main(String args)and because of this, I received the error message.
稍后编辑::) 我的错误:我的主要方法如下所示:public static void main(String args)并且因此,我收到了错误消息。
采纳答案by Joey
I suspect that the signature of your method is wrong. It has to be exactly:
我怀疑你的方法的签名是错误的。它必须是:
public static void main(String[])
otherwise it won't work. Naming the method Main
will fail, omitting the argument to the method will fail. Also, I think the class needs to be public.
否则它不会工作。命名方法Main
将失败,省略方法的参数将失败。另外,我认为课程需要公开。
回答by Ham Vocke
Are you sure that you have a method of the typepublic static void main(String[] args)
?
你确定你有一个类型的方法public static void main(String[] args)
吗?
You should check if you have no typos in your code. Maybe you could post an example of your main-method's head.
您应该检查代码中是否没有拼写错误。也许您可以发布一个主要方法头部的示例。
回答by Zach Rieck
One shortcut that really helps me, Eclipse has syntax suggestions, and if you type "main" into the terminal window and hit control + space, you can choose main method and it'll populate it complete with enclosed braces. Helpful if you're not always quick to remember syntax off the top of your head!
一个真正对我有帮助的快捷方式,Eclipse 有语法建议,如果你在终端窗口中输入“main”并点击 control + space,你可以选择 main 方法,它会用括起来的大括号填充它。如果您不能总是快速记住语法,这将很有帮助!