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.do开发者_Python百科Privileged(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 :(
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.
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
The required mail implementation jars should be present in your classpath.
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.
First add JavaMail API 1.4.7 jar to the class path.
Then add the same jar to server library. In my application i used Apache tomcat 7. i put the jar in lib folder
精彩评论