Java - Could not find the main class?
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.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 04, 2022Comments
-
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 about 14 yearsI 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 about 14 yearsI would suggest comparing jars created with ip2c library and without it. Is
MANIFEST.MF
the same? And istestPackage.testFrame
compiled successfully in both cases (this was suggested by ring bearer in his answer)? -
Admin about 14 yearsWell 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 about 14 yearsAre 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 about 14 yearsGood 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 about 14 yearsHave you figured out what was wrong with your jar? I am curious.
-
Admin about 14 yearsThanks 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.