75

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.

I get an error that says 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.

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!

jww
  • 97,681
  • 90
  • 411
  • 885
user2321611
  • 1,049
  • 1
  • 7
  • 17
  • 13
    Do you have a `public static void main(String[] args)` anywhere? –  Apr 25 '13 at 21:40
  • 2
    I do have public class "name" extends JFrame – user2321611 Apr 25 '13 at 21:43
  • 3
    i dont know what causes this, or how to solve it, but it sure has happened to me with public classes that do have a main method (correctly declared of course). Just wanted to point out that it may not be as simple as you might think – jambriz Apr 25 '13 at 21:54
  • 2
    I think all the probes point that he's trying to run some classes without a main method. We'll continue investigating. – aran Apr 25 '13 at 21:55
  • 1
    Maybe it is a maven project so click in the name of your project and hit the follow keys alt+f5 to update the project. – André Luís Tomaz Dionisio Jan 05 '18 at 16:36
  • Ten years later, and I receive it on version 2022-06 of eclipse. Running on Linux Ubuntu 22.04. Was there ever a viable answer among the many given? Looks like a lot of shot-iin-the-dark responses. – TheGeeko61 Jul 12 '22 at 00:57

25 Answers25

93

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

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

If the option 'Use as Source Folder' is not visible then inside the 'Build Path' select the option 'Remove from Build Path'. This will allow 'Use as Source Folder' option to appear in the 'Build Path'.

Community
  • 1
  • 1
Rashid
  • 931
  • 1
  • 6
  • 2
  • this worked for me. I had the source code for Java Fundamentals from Deital...ti has chapters has folders and java code inside it. I moved all of them within source but still I need to do this step to make it work. – Ashu Sep 06 '18 at 02:12
  • The Source Folder and target folder (where **Eclipse** puts your main class) are different things. You mean "Where you put the source file for main class". – Gangnus Apr 24 '19 at 05:06
  • This helped me solve the problem when i had installed eclipse and was running the program for the first time – Devrath Jul 28 '19 at 06:58
  • That changed the package of all class files to be "default package".. – nsandersen Jan 12 '21 at 11:31
48

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.

zen
  • 591
  • 1
  • 4
  • 5
28

I hope you are trying to run the main class in this way, see screenshot:
screenshot of Eclipse file context menu

If not, then try this way. If yes, then please make sure that your class you are trying to run has a main method, that is, the same method definition as below:

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

I hope this will help you.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
6

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

mishal153
  • 1,498
  • 3
  • 26
  • 37
5

The entry point for Java programs is the method:

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

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

3

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.
Eyal Sooliman
  • 1,876
  • 23
  • 29
3

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).

Yang Zhang
  • 31
  • 2
3

I am running eclipse from Ubuntu. Had this same problem and was able run the program through terminal. So I just moved the existing public static void main(String[] args) { just below the class declaration (it got automatically formatted by eclipse) and the next launch was successful. Then moved the main method back to where it was before and it worked fine this time.

hipokito
  • 413
  • 4
  • 11
  • This answer neatly points to some Eclipse bug and successfully works it around. Seemingly nonsense action mentioned in the answer worked for me even if cleaning the project did not work and all the other things were all right. It looks that Eclipse sometimes holds some invalid class state even after cleaning the project and it initiates the refresh only after the Java file is edited and saved. Context: I copied some Java files over existing ones and then the issue started. – miroxlav Nov 07 '18 at 21:05
3

I had this problem in two projects. Maven and command line worked as expected for both. The problems were Eclipse specific. Two different solutions: Project 1): Move the main method declaration to the top within the class, above all other declarations like fields and constructors. Crazy, but it worked. Project 2): The solution for Project 1) did not remedy the problem. However, removing lombok imports and explicitly writing a getter method solved the problem

Conclusion: Eclipse and/or the lombok plugin have/has a bug.

Øyvind Roth
  • 217
  • 1
  • 2
  • 11
  • I had the same problem with an AWS SDK package import. I had to remove `import com.amazonaws.regions.Regions;` from the main class to be able to run the app from Eclipse. I'm using AWS Java SDK v. 1.11.729. – gparis Feb 26 '20 at 11:17
3

Looks too late to answer but might help someone,

Having same problem i solved it by following steps:::::

Select Main class in eclipse then click on Window in menu bar,

Window-->Show view-->Select Outline

Right click on main(String[]):void then Run As --> java Application

Image reference for Running Main method

By doing this you can run the main method directly. This worked for me

Zakir Hussain
  • 359
  • 2
  • 13
2

Right Click > Run AS > Run Configurations

In this screen if your "Main class" Text field is empty, then add the class name by clicking "Search" button on the right side of the text field and choose the class file. And then click "Run" button on the bottom of the configuration screen. That's it

Yithirash
  • 377
  • 3
  • 6
  • 18
1

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

Dev M
  • 1,519
  • 3
  • 29
  • 50
1

Make sure the main in public static void main(String[] args) is lower case. For me it didn't work when I had it with capital letter.

sander
  • 1,426
  • 4
  • 19
  • 46
1

Put your Main Java class file in src/main/java folder and check if there is not any error in 'Java Build Path' by following right click on project and select Java Build Path->Source.

Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26
Rajiv Ranjan
  • 333
  • 4
  • 13
1

If you are working with a Maven project you have to understand the fact that directory layout is bit different. In this the package name must be src/main/java.

For this update your source folder by right click on project root folder -> properties -> java build path -> source tab. Here remove all other source folders as they might have been added in wrong manner. Now, select project /src/main/java as the source folder. Add and apply the changes. Now refresh your workspace using F5.

This should fix the issue of not recognizing a main type.

Abhishek Rathore
  • 1,016
  • 1
  • 11
  • 13
1
  1. ---> Right click on the projecct and select properties. enter image description here

  2. ---> Select Java Build Path

enter image description here

  1. ---> Select Source tab, see source folders on build path is pointing to your project/src path. If not, select Add folder and select the src folder.

enter image description here

  1. ---> Under Default output folder see that it is pointing to your ProjectName/src

enter image description here


Solution - 2

Or Try this way - https://stackoverflow.com/a/76375396/11962586

S14321K
  • 220
  • 3
  • 19
0

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!

shivesh suman
  • 1,511
  • 18
  • 26
0

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 main method, 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 main method 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.

Keven M
  • 972
  • 17
  • 47
0

When you save your file, make sure it has the extension .java. If it does not, Eclipse won't know to read it as a java file.

KC McLaughlin
  • 246
  • 5
  • 12
0

I had this issue because the tutorial code I was trying to run wasn't in the correct package even though I had typed in the package name at the top of each class.

I right-clicked each class, Refactor and Move To and accepted the package name suggestion.

Then as usual, Run As... Java Application.

And it worked :)

likejudo
  • 3,396
  • 6
  • 52
  • 107
0

You must check this as well, Go to Java build path -> config build path, check that JRE System Library [Java SE -version] is check marked, try running the code again. This fixed my issue.

0

In my case I was using ant to perform the build, and clearly had the class setup in the build.xml file:

<target name="jar">
  <jar destfile="ec-stats.jar" includes="bin/**,src/**">
    <manifest>
        <attribute name="Main-Class" value="my.package.MyStatistics" />
    </manifest>
  </jar>
</target> 

But it still kept giving me the same error, until I right-clicked on the folder in Eclipse and chose Build Path -> Use As Source Folder.

enter image description here

See also Error: Selection does not contain a main type

user8128167
  • 6,929
  • 6
  • 66
  • 79
0

Solved the issue as following:

  1. Going in Properties of the Project
  • 1.1) Inside "Java Compiler": chose the: "Compiler compliance level:" to a specific version of JDK (in my case I choosed 15)

  • 1.2) Inside "Java Build Path", in the tab "Libraries", at the Modulepath: change the "JRE System Library" to the same of the version you choosed at step 1.1 above (in my case I picked JDK 15)

  1. Run the java file that contains a main method
0

This can be resolved in two steps if you are using Eclipse IDE.

Step I: Right click on the project and click Maven > Choose Up-date Project

Step II: Select the project the click Ok button

enter image description here

Hope this helps.✔

0

This error usually occurs because jdk is not selected for the project. I had the same problem and this worked for me.

Make sure that your class file is inside src folder then follow below steps

Step 1: Right click on project folder Step 2: Build Path --> Configure Build Path Step 3: Select "Java Build Path" from the left corner of the window Step 4: Under "Order and Export" Tab Step 5: Click on any of the jdk available check box to select it. Step 6: Click on Apply and Close.