score:28

Accepted answer

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.

score: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!

score: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.

score: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.

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

score: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.

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

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

score: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.โœ”

score:1

you must place all your files (file.java) under the root folder src.

score: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.

score: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.

score: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.

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

score: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.

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

score: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.

score: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.

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

score: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.

score:5

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

score: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.

score:87

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


Related Query

More Query from same tag