Java 编辑器在 Eclipse 中不包含主类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24117713/
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
Editor does not contain a main type in Eclipse
提问by ThisGuy
I downloaded eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz
. This eclipse is built-in with java and my Lubuntu is 64-bit. Whenever I compile and run a simple code in java like this one below:
我下载了eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz
。这个 eclipse 是用 java 内置的,我的 Lubuntu 是 64 位的。每当我在 Java 中编译和运行一个简单的代码时,如下所示:
public class Sample{
public static void main(String[] args){
System.out.println("YOLO");
}
}
I always get an Editor does not contain a main type
.
I put the file in a project folder called Sample
. This eclipse should compile java code because its an IDE distribution specialized for java.
我总是得到一个Editor does not contain a main type
. 我将文件放在一个名为Sample
. 这个 eclipse 应该编译 java 代码,因为它是一个专门用于 java 的 IDE 发行版。
How can i resolve this error?
我该如何解决这个错误?
Any help would be much appreciated.
任何帮助将非常感激。
Here's my project structure:
这是我的项目结构:
回答by Ninad Pingale
Right click your project > Run As > Run Configuration... > Java Application (in left side panel) - double click on it. That will create new configuration. click on search button under Main Class section and select your main class from it.
右键单击您的项目 > 运行方式 > 运行配置... > Java 应用程序(在左侧面板中) - 双击它。这将创建新的配置。单击主类部分下的搜索按钮,然后从中选择您的主类。
回答by Cyclopes
Make sure you do Run As > Java Application.
确保您执行 Run As > Java Application。
If not you could try a Project > Clean
如果没有,您可以尝试 Project > Clean
Some more questions that deals with this that could be helpful, Refer this
处理此问题的更多问题可能会有所帮助, 请参阅此
回答by phil_20686
I suspect the problem is that Sample.java should be in a package inside the src folder.
我怀疑问题是 Sample.java 应该在 src 文件夹内的一个包中。
I guess that eclipse will not automatically look outside of there.
我猜 eclipse 不会自动看外面。
回答by sarath
Create a source folder under Java Resources
在 Java Resources 下创建一个源文件夹
回答by ThatOneGuy
I just had this exact same problem. This will sound crazy but if someone sees this try this before drastic measures. delete method signature:
我刚刚遇到了完全相同的问题。这听起来很疯狂,但如果有人看到这个,请在采取严厉措施之前尝试一下。删除方法签名:
public static void main(String args[])
(Not the body of your main just method declaration)
(不是您的主要方法声明的主体)
Save your project then re-write the method's header back onto its respective body. Save again and re-run. That worked for me but if it doesn't work try again but clean project right before re-running.
保存您的项目,然后将方法的标题重新写入其各自的主体。再次保存并重新运行。这对我有用,但如果它不起作用再试一次,但在重新运行之前清理项目。
I don't know how this fixed it but it did. Worth a shot before recreating your whole project right?
我不知道这是如何解决的,但确实如此。在重新创建整个项目之前值得一试吗?
回答by Shiyas Cholamukhath
Ideally, the source code file should go within the src/default package even if you haven't provided any package name. For some reason, the source file might be outside src folder. Create within the scr folder it will work!
理想情况下,即使您没有提供任何包名,源代码文件也应该放在 src/default 包中。出于某种原因,源文件可能在 src 文件夹之外。在 scr 文件夹中创建它将起作用!
回答by Pradeep AR
I had the same issue. I had accidentally deleted the .classpath and .projectfile in my workspace. Luckily it was in Recycle bin, as soon as it was restored, there were no issues.
我遇到过同样的问题。我不小心删除了工作区中的.classpath 和 .project文件。幸运的是它在回收站中,一旦恢复,就没有问题。
回答by Pavan Yogi
Right click on Sample.java file and delete it. Now go to File -> New -> Class , enter name of program (i.e. hello) , click on finish . It will create file hello.java. Enter source code of program and finallly press ctrl + F11
右键单击 Sample.java 文件并将其删除。现在转到 File -> New -> Class ,输入程序名称(即 hello),单击完成。它将创建文件 hello.java。输入程序的源代码,最后按 ctrl + F11
回答by Piyush Aggarwal
I installed Eclipse and created a Java project. Created new Java file outside the 'src' directory and tried to run that. I got the same error "Editor does not contain a main type". I just moved the java file into the 'src' folder and could simply run the program.I couldn't understand what other answers were asking to try. It was so simple.
我安装了 Eclipse 并创建了一个 Java 项目。在“src”目录外创建了新的 Java 文件并尝试运行它。我遇到了同样的错误“编辑器不包含主要类型”。 我只是将 java 文件移动到“src”文件夹中,并且可以简单地运行该程序。我无法理解其他答案要求尝试什么。太简单了。
回答by Sonu Mishra
Just change "String[] args" to "String args[]".
只需将“String[] args”更改为“String args[]”。