Java 错误:选择不包含主要类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16225177/
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-16 05:52:02  来源:igfitidea点击:

Error: Selection does not contain a main type

javaeclipseprojectstartupstartup-error

提问by user2321611

I am trying to run some java files in a new project. So I make the project, put the files in it and I try to run the main file so my game starts.

我正在尝试在新项目中运行一些 java 文件。所以我制作了这个项目,把文件放进去,然后我尝试运行主文件,这样我的游戏就开始了。

I get an error that says selection does not contain a main type.

我收到一个错误,说selection does not contain a main type.

I have tried several ways to run it:

我尝试了几种方法来运行它:

  • Some say to launch eclipse again, tried this a dozen times.
  • Somewhere else someone pointed to open a new project and make a build path to the old project.
  • 有人说要再次启动eclipse,试了十几次。
  • 在其他地方有人指出打开一个新项目并为旧项目创建一个构建路径。

Didn't work either.

也没有用。

I am pretty sure it must work because I ran it a few hours ago at school. How do I get this working? Thank you in advance!

我很确定它一定能用,因为我几个小时前在学校运行过它。我如何让这个工作?先感谢您!

采纳答案by NoNaMe

Hope you are trying to run the main class in this way see screen shot: enter image description here

希望您尝试以这种方式运行主类,请参见屏幕截图: 在此处输入图片说明

If no then try this way and if yes then please make sure that your class you are going to run have main method i-e

如果不是,那么尝试这种方式,如果是,那么请确保您要运行的类具有 main 方法,即

public static void main(String[] args){ // some code here}

same definition. Hope this will help you.

相同的定义。希望这会帮助你。

回答by user2321611

The entry point for Java programs is the method:

Java 程序的入口点是方法:

public static void main(String[] args) {
    //Code
}

If you do not have this, your program will not run.

如果你没有这个,你的程序将不会运行。

回答by zen

The other answers are all valid, however, if you are still having a problem you might not have your class inside the src folder in which case Eclipse may not see it as part of the project. This would also invoke the same error message you have seen.

其他答案都是有效的,但是,如果您仍然遇到问题,则 src 文件夹中可能没有您的类,在这种情况下,Eclipse 可能不会将其视为项目的一部分。这也会调用您所看到的相同错误消息。

回答by Dev M

You must place all your files (file.java) under the root folder SRC.

您必须将所有文件 (file.java) 放在根文件夹SRC 下

回答by Eyal Sooliman

Few things to check out:

需要检查的几件事:

  1. Do you have a main package? do all of your classes are under this package?
  2. Do you use a main class with public static void main(String[] args)?
  3. Do you declare: package ; in your main class?
  4. You can always clean the project before running it. In Eclipse - Just go to Project -> clean then run the app again.
  1. 你有主包吗?你所有的课程都在这个包下吗?
  2. 您是否使用带有 public static void main(String[] args) 的主类?
  3. 你是否声明:包;在你的主班?
  4. 您始终可以在运行之前清理项目。在 Eclipse 中 - 只需转到 Project -> clean 然后再次运行应用程序。

回答by Rashid

Right click on the folder where you put your main class then click on Build Path--> Use as Source Folder.

右键单击放置主类的文件夹,然后单击Build Path--> Use as Source Folder

Finally run your main file as java application. Hope this problem will be solved.

最后将主文件作为 java 应用程序运行。希望这个问题会得到解决。

回答by mishal153

I resolved this by adding a new source folder and putting my java file inside that folder. "source folder" is not just any folder i believe. its some special folder type for java/eclipse and can be added in eclipse by right-click on project -> properties -> Java buld path -> Source and add a folder

我通过添加一个新的源文件夹并将我的 java 文件放在该文件夹中解决了这个问题。“源文件夹”不仅仅是我相信的任何文件夹。它是 java/eclipse 的一些特殊文件夹类型,可以通过右键单击项目 -> 属性 -> Java buld 路径 -> 源并添加文件夹来添加到 eclipse 中

回答by shivesh suman

I ran into the same issue and found that there was an extra pair of braces (curly brackets) enclosing public static void main(String args) { ... }. This method should really be at the top scope in the class and should not be enclosed around braces. It seems that it is possible to end up with braces around this method when working in Eclipse. This could be just one way you can see this issue when working with Eclipse. Happy coding!

我遇到了同样的问题,发现有一对额外的大括号(大括号)封闭了 public static void main(String args) { ... }。这个方法真的应该在类的顶级范围内,而不应该用大括号括起来。在 Eclipse 中工作时,似乎有可能在此方法周围使用大括号。这可能只是您在使用 Eclipse 时看到此问题的一种方式。快乐编码!

回答by Keven M

I had this happen repeatedly after adding images to a project in Eclipse and making them part of the build path. The solution was to right-click on the class containing the mainmethod, and then choose Run As -> Java Application. It seems that when you add a file to the build path, Eclipse automatically assumes that file is where the mainmethod is. By going through the Run As menu instead of just clicking the green Run As button, it allows you to specify the correct entry-point.

在 Eclipse 中将图像添加到项目并将它们作为构建路径的一部分后,我反复发生这种情况。解决方案是右键单击包含该main方法的类,然后选择 Run As -> Java Application。似乎当您将文件添加到构建路径时,Eclipse 会自动假定该文件是main方法所在的位置。通过浏览运行方式菜单而不是仅仅单击绿色的运行方式按钮,它允许您指定正确的入口点。

回答by Yang Zhang

I ran into the same problem. I fixed by right click on the package -> properties -> Java Build Path -> Add folder (select the folder your code reside in).

我遇到了同样的问题。我通过右键单击包 -> 属性 -> Java 构建路径 -> 添加文件夹(选择您的代码所在的文件夹)来修复。