Troncoso
Troncoso

Reputation: 2463

Error when creating jar file: NoClassDefFoundError

My code compiles correctly in Eclipse, as well as runs. Before I added a piece of code I could also successfully make a jar file by doing:

jar cvfm Manifest.txt <Classes>

then I added this to my code in the constructor:

addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
    JFrame window = (JFrame) e.getSource();
            window.toBack();
}});

When I try to create a new jar file and run it, I receive the NoClassDefError with the error line pointing to that code. To be specific, I got this:

Exception in thread "main" java.lang.NoClassDefFoundError: BinaryClock$1
at BinaryClock.<init>(BinaryClock.java:55)

BinaryClock being my main class and line 55 being the first line of the code from above. I don't understand why it makes BinaryClock$1, then gives me the error on it.

If more code or information is needed, let me know. I didn't want to paste my entire source code if it wasn't needed.

Upvotes: 2

Views: 603

Answers (2)

allprog
allprog

Reputation: 16780

You can create jar files from Eclipse directly. Right click on your project in Package Explorer -> Export... -> Java -> JAR file. This is very convenient because it considers all your settings on the project including the classpath and lets you specify a ton of additional options. Moreover, you can save it in an Ant file which can be run with the External tools. More info here: http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

The BinaryClock$1.class file will contain the anonymous inner class created for your WindowAdapter in the code you've shown. You should include that file in the jar file.

Basically, you should build into a clean directory, and include everything in that directory in your jar file. Don't try to be selective about it - if a file is being produced by the compiler, there's a good reason for it.

Upvotes: 4

Related Questions