Java error using JARs, java.lang.NoClassDefFoundError: javax/mail/Store

*爱你&永不变心* 提交于 2019-12-11 02:55:22

问题


I'm making a java application that uses Javamail, and it works great when I compile it. I want to make it into a jar file so it can be easily moved around and executed. The problem is I am getting this error when I try to run the jar from the cmd line

java -jar ActriveTray2.jar

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Store
        at stockApp.init(stockApp.java:11)
        at ActiveTray.main(ActiveTray.java:31)
        Caused by: java.lang.ClassNotFoundException: javax.mail.Store
        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)

Here's what's in my jar file

  • images/tray.gif
  • META-INF/MANIFEST.MF
  • ActiveTray.class
  • ActiveTray.java
  • config.class
  • config.java
  • GmailFetch.class
  • GmailFetch.java
  • stockApp$1.class
  • stockApp.class
  • stockApp.java

I think the issue is classpath, where the jar doesn't know where the javamail jar lives. My classpath is set correctly when I execute the code (java ActiveTray2) it works fine... help :(


回答1:


you need to add the jars you depend on to the manifest file, and also add them to the jar you are creating. java -jar will ignore your environment's classpath, by design. From the java docs:

-jar: When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

so, you can add a line to your manifest file like:

Class-Path: mail.jar

to your manifest file.




回答2:


When you launch a runnable jar file in java, the classpath is ignored, both the environment variable and the command line classpath. You have to specify it inside the manifest file of your runnable jar file.

Here is some information from wikipedia :

You manifest has to include :

Main-Class: org.mypackage.HelloWorld
Class-Path: lib/supportLib.jar
<additional line feed and carriage return>

The class-path will list the location of the librairies you use relative to the location of your main runnable jar file.

Regards, Stéphane




回答3:


The required mail implementation jars should be present in your classpath.




回答4:


Same problem i faced in my project. The reason is jar file missing or not locate in class path. Two steps to solve this issue.

  1. First add JavaMail API 1.4.7 jar to the class path.

  2. Then add the same jar to server library. In my application i used Apache tomcat 7. i put the jar in lib folder



来源:https://stackoverflow.com/questions/6472202/java-error-using-jars-java-lang-noclassdeffounderror-javax-mail-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!