开发者

java.lang.NoClassDefFoundError in command line

开发者 https://www.devze.com 2023-02-04 08:26 出处:网络
I\'m developing an application in Eclipse and it runs fine from within Eclipse. The problem I\'m having is that when I export it to a jar file and run it from the command line I get a NoClassDefFound

I'm developing an application in Eclipse and it runs fine from within Eclipse. The problem I'm having is that when I export it to a jar file and run it from the command line I get a NoClassDefFound error for javax.mail.internet.

In both my project build path and class path I have included the activation.jar and mail.jar libraries required for me to use javax.mail.internet, and like I said it works fine from within Eclipse but not when I export it to a jar. If my build path has those files and so does my class path why would this not be working?

Here is the error stack: Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/mai l/internet/InternetAddress at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour ce) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unk开发者_Python百科nown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: javax.mail.internet.InternetAddress at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 27 more


This is almost certainly a classpath issue. Try opening the JAR you exported (via winzip, or rename to a .zip and use windows) and either examine the jarred folder structure to determine that "javax/mail/internet/" exists and the InternetAddress.class file is contained with it. Or open the MANIFEST.MF located within the JAR to determine that it contains a classpath entry and the jars specified in the manifest exist in the locations specified by the manifest.

Edit: Examine your runtime classpath by printing it from the system property in the JVM.

System.out.println(System.getProperty("java.class.path"));

I think you will find it missing, sorry to make you go through the extra work of manual inspection, but it is sometimes revealing.

Typically jar files are not contained inside a jar. There are two common ways to export an executable jar:

  1. Jar your class files and include a manifest that specifies the classpath where the JVM can find other libraries relative to your own.
  2. Jar your class files along with the contents of other jars into a single jar. This is most likely what you want and can be accomplished by using eclipse's "export... > Runnable JAR File" wizard. Be sure to use "Runnable JAR File" and select the right run configurations.

You might find this SO question relevant: Classpath including JAR within a JAR


Wow I feel like a complete idiot. All you have to do is when exporting to a jar file instead of choosing "JAR File" in Eclipse choose "Runnable JAR File" and then tell it to package the required libraries in it to keep it small and wa-la, everything works nicely.

Another options is to add this line in the manifest file when the jar is being created: Class-Path: lib/mail.jar

And then just have a lib folder with mail.jar in it within the same folder that the exported jar will be in. Boom.

The former option is the best in my opinion.


One possibility would be that something that class that javax.mail package depends on (aka transitive dependency) was missing. But I would first double-check classpath; java command line executable does not indicate missing directories (or non-existing referred jars) at all -- I have sometimes mixed up semicolon and colon as path separator, and this results in classes not being found.

One nice thing JDK 1.6 finally had was ability to define wildcards, such that you can do:

  java -cp lib/\*

(backslash used to quote asterisk so unix shell will not expand it)

which would include all jars from directory 'lib/'; so that you do not have to manually add all individual jars in that directory.


add mail.jar to your classpath, and make sure your starting directory is the parent of lib (the latter seems the most probable issue). you can start your main method w/ smth like:

System.getProperties().store(System.out, "boot props");
0

精彩评论

暂无评论...
验证码 换一张
取 消