No Main Class Found

If Java comes up with the error message like “no main class found” you might have a problem like many many others before (as a google query reveals). Usually, the problem comes from one of the following issues:

  • non public class containing the main method
  • main method not formed correctly (consult the java tutorial for help)
  • classpath not set up correctly, so that the main class could not be found
  • in case of running a JAR:
    • the manifest file might point to the wrong main class. See the Java tutorial for help.
    • the manifest’s classpath might be set up wrong. See the Java tutorial for help.
    • ensure that the file manifest.mf is in the base folder of the project. It contains by default:
      Manifest-Version: 1.0
      X-COMMENT: Main-Class will be added automatically by build
    • Ensure the following lines are present in file project.properties (within folder “nbproject”):
      main.class=yourMainClass
      manifest.file=manifest.mf
  • still no succcess? If you’re using NetBeans, read on below! Otherwise, google might be helpful, sorry and good luck.
  • Did you use NetBeans (prior to v7) and moved the main class? An already fixed bug did not update your run configuration when the Class containing the main method was moved (not renamed).

When you want to run a NetBeans project, once in a while (~once a year in my case) you might receive a “no Main classes found”. In that case you might want to try the following steps:

  1. Ensure you have a public class containing a main method
  2. This class must have a well defined main method: “public static void main(String[] args) { }
  3. You have set up the project configuration with the correct class: Project Properties > Run > Main Class (maybe just set it again, so that the project properties file is written again)
  4. Try to rebuild the project: Menu > Run > Clean & Build Main Project
  5. Still no success? Try renaming the class (Foo -> Foo1) and rename it back (Foo1 -> Foo). This should invalidate the NetBeans cache for this file (Just assuming – I am not a NetBeans Developer).
  6. Still no success?? You might to have to clear the NetBeans cache by deleting the directory “<user home>.netbeans6.8varcache” (the 6.8 might of course vary according to your version of NetBeans). Close NetBeans before deleting the cache – the next start might take some time as NB will most likely have to scan your code again.
  7. Still no luck? You might want to consult the NetBeans user forum or nbusers-Mailinglist (please be friendly, patient and report the bug in a way that others are able to help).

7 thoughts on “No Main Class Found”

  1. From my experience, I would say the common error is that the classpath is not set up correctly or the manifest file is wrong. I bet every java developer knows this problem 😉

    1. Oh right. Thank’s for the hint. Just added it to the list.

  2. netbeans for me is by opening each class on that project, edit each of it, either by adding space and the delete it again, or just add a newline for each class. After all class have been edited, save it, and then run.
    it works for me.
    cheers.

    1. Ouch! I hope you’ll never have to do this on a bigger project. Deleting the cache and rescanning might be faster depending on the project size.

  3. I have been landed here after searching for a solution to let Netbeans create a proper manifest file in the generated jar file. This file was missing in several projects, but not in all. In this case you can’t “start” your application by simply give the jar file to java.exe like “java -jar YourApp.jar”. Every project have a main class set, but however Netbeans does not create it.
    After comparing those manifest-less projects with other ones I found the difference, which has fixed my issue:

    1. You have to ensure the file “manifest.mf” is in the base folder of the project. It contains by default:
    Manifest-Version: 1.0
    X-COMMENT: Main-Class will be added automatically by build

    2. Ensure the following line(s) is(are) present in file “project.properties” (within folder “nbproject”):
    main.class=yourMainClass
    manifest.file=manifest.mf

    Hope this helps someone.

  4. I was pulling hair for hours trying to figure out why I couldn’t run my jar until I finally realized that I was developing with JDK 7, but didn’t have JRE 7 installed on my machine. It’s always the obvious things *sigh*, but this post helped me figure that out, so thank you very much!

Comments are closed.