[java] Error: Selection does not contain a main type

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!

This question is related to java eclipse project startup startup-error

The answer is


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


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.


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.


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!


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.


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.


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.


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


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.


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.

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


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


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.


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


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.


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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to project

How to create a Java / Maven project that works in Visual Studio Code? IntelliJ does not show 'Class' when we right click and select 'New' error C2220: warning treated as error - no 'object' file generated Android Studio: Default project directory Error: Selection does not contain a main type How to open an existing project in Eclipse? The project was not built since its build path is incomplete Eclipse projects not showing up after placing project files in workspace/projects Method to find string inside of the text file. Then getting the following lines up to a certain limit "Sources directory is already netbeans project" error when opening a project from existing sources

Examples related to startup

Error starting Tomcat from NetBeans - '127.0.0.1*' is not recognized as an internal or external command Spring Boot application as a Service Run Batch File On Start-up how to start the tomcat server in linux? Error: Selection does not contain a main type Run automatically program on startup under linux ubuntu How do I start my app on startup? How to run a program automatically as admin on Windows 7 at startup? How to run a C# application at Windows startup? Cannot run Eclipse; JVM terminated. Exit code=13

Examples related to startup-error

Error: Selection does not contain a main type