Java - Could not find the main class?

12,017

Solution 1

If I comment the class that is using the ip2c.jar file, I don't get the error.

Is it that when you comment above mentioned usage, some classes get compiled properly? and when you include, you have some error, that causes the .class for your Main class not be generated? - just a hint as a third-party jar would not interfere with META-INF/MANIFEST.MF in main jar.

Solution 2

If you want to run your application by clicking on the jar you need to specify the main class in META-INF/MANIFEST.MF file in the jar as it is described here. Make sure you have it properly defined.

You should be able to do this in Netbeans during creation of your jar file.

You may also find this question in SO helpful.

Solution 3

I also had this problem. It happens that Java does not tell you the ERROR correctly. Whereas my Main class was correct, it told me that my Main class was not found. My error was that, I had not copied a new library to my lib folder outside of Netbeans. Hope you get this..I mean, I created a folder to run my JAR file, in this folder, I have a lib folder that I copied from my project. This may sound not very clear, If anyone has this error, I can help you out. Thanks.

Share:
12,017
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin about 2 years

    I'm having a hard-time with a Java error. I'm getting this when double-clicking on my compiled project Jar file in order to run it:

    Could not find the main class: testPackage.testFrame. Program will exit.
    

    However, my main class is cleary defined in the project properties. Furthermore, everything works fine when I run the project directly from NetBeans.

    I'm using the ip2c.jar third-party library to determine countries from IP addresses. If I comment the class that is using the ip2c.jar file, I don't get the error.

    How can a third-party library be preventing the JVM from finding my main class?

  • Admin
    Admin about 14 years
    I already said that "my main class is cleary defined in the project properties". Also, the manifest is being automatically created by NetBeans. Besides, this isn't the cause of my problem as everything works fine when I'm not using the ip2c external library.
  • Piotr
    Piotr about 14 years
    I would suggest comparing jars created with ip2c library and without it. Is MANIFEST.MF the same? And is testPackage.testFrame compiled successfully in both cases (this was suggested by ring bearer in his answer)?
  • Admin
    Admin about 14 years
    Well I'm sure that this third-party lib is the source of my problem. However, it doesn't prevent my Main .class from being generated, since I can see it when I extract the Jar.
  • ring bearer
    ring bearer about 14 years
    Are you getting this error when you double click on the JAR?? in that case, you may give command line a shot, go to command line, make sure java is in path, change dir to location where your JAR file is saved, and type "java -cp"path to all dependencies" -jar YourProject.jar" - this may give you more detailed error.
  • Admin
    Admin about 14 years
    Good idea. In both Jars, all the .class files are present. The manifests are identical, byte per byte. Actually, the only thing that differs from one Jar to the other is the file size of some .class files, and the fact that one gives me the error and the other doesn't.
  • Piotr
    Piotr about 14 years
    Have you figured out what was wrong with your jar? I am curious.
  • Admin
    Admin about 14 years
    Thanks to your idea, I solved my problem. You were right, I got more details in the console. There was a NullPointerException thrown that prevented the JVM from going further.