Editor does not contain a main type in Eclipse [duplicate]

社会主义新天地 提交于 2019-11-28 18:38:59

Problem is that your folder is not identified as a Source folder.

  1. Right click on the project folder -> Properties
  2. Choose 'Java Build Path'
  3. Click on 'Sources' tab on top
  4. Click on 'Add Folder' on the right panel
  5. Select your folders and apply

I suspect the problem is that Sample.java should be in a package inside the src folder.

I guess that eclipse will not automatically look outside of there.

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.

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?

Cyclopes

Make sure you do Run As > Java Application.

If not you could try a Project > Clean

Some more questions that deals with this that could be helpful, Refer this

Create a source folder under Java Resources

For me, classpath entry in .classpath file isn't pointing to the right location. After modifying it to <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> fixed the issue

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!

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

Right click on your project, select New -> Source Folder

Enter src as Folder name, then click finish.

Eclipse will then recognize the src folder as containing Java code, and you should be able to set up a run configuration

First look for the main method is there or not.If it is there, do restart your eclipse and right click on the page which having main method, Go to run as Java application.

I had the same issue. I had accidentally deleted the .classpath and .project file in my workspace. Luckily it was in Recycle bin, as soon as it was restored, there were no issues.

place your main method class in src folder(in Eclipse Environment).

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.

Just change "String[] args" to "String args[]".

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!