Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError in simple .jar file

Tags:

java

jar

I'm trying to create a simple .jar file out of my project. The project is made of two .class files - the main class which uses the secondary class to generate a GUI. The main class is the actual "main" class that runs while the second class is just a class file with it's methods and it's also an extension of JFrame and imports javax.swing and java.awt.event.*.

I use Jar to bundle it all up. I add a manifest file (with a new line character) which points to the primary file with the main method. The Jar file thus has two .class files and a folder with the manifest.txt in it. When I use javaw.exe to run it, nothing happens at all. So I try to run it in the command line and I get a NoClassDefFroundError about the secondary class.

I noticed I get the same kind of error when I try to compile and run the second class in JCreator - no wonder, it doesn't have a main method, it's just a class file. When I run the main file from JCreator, everything works fine.

Any ideas?

like image 956
Protagonist Avatar asked Dec 05 '25 01:12

Protagonist


2 Answers

Looking at your stack trace, I can now see the problem: I can tell you actually have more than two classes:

Caused by: java.lang.ClassNotFoundException: grafPrime$calcButton at 

There's a file named grafPrime$calcButton.class, and it needs to be in the jar file, too. There may be other such files -- make sure you include all of them!

like image 64
Ernest Friedman-Hill Avatar answered Dec 07 '25 17:12

Ernest Friedman-Hill


Okay, the problem is that you've not included the anonymous class - you should have a file called grafPrime$calcButton.class, and that's not in your jar file.

Basically, compile your code into a clean directory and include all the class files which are generated.

like image 43
Jon Skeet Avatar answered Dec 07 '25 15:12

Jon Skeet