tags:

views:

213

answers:

4

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?

+6  A: 

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.

Piotr
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.
asmo
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)?
Piotr
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.
asmo
+1  A: 

You need to ensure your manifest is correct and in the correct directory (which appears to be fine given then error message contains a class name), and that the class is in the JAR, and packaged with a correct directory structure which matches its package structure.

dty
+2  A: 
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.

ring bearer
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.
asmo
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.
ring bearer
Have you figured out what was wrong with your jar? I am curious.
Piotr
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.
asmo
+1  A: 

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.

kchmweno