5

I am using javax.mail .jar file to read the mail messages. But when i m running the code i am getting the following exception.

I added mail.jar in classpath.

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingE
xception
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        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)
Could not find the main class: MasterProcess.  Program will exit.

any suggetionsn to solve this issue please ...

13
  • Are you sure mail.jar is on the classpath? How did you start the application? Commented Sep 2, 2011 at 10:53
  • I am runnign through command prompt : Commented Sep 2, 2011 at 11:01
  • using the command javac -cp %classpath% Test.java then java -cp . Test to run the application Commented Sep 2, 2011 at 11:01
  • And what does %classpath% contain? Commented Sep 2, 2011 at 11:09
  • 1
    You say you are running with java -cp . Test , you need to include the .jars explicitly in your classpath here when running the application too. Not just when compiling. Commented Sep 2, 2011 at 11:50

4 Answers 4

1

You're not getting a MessagingException, the VM is complaining that it can't find MessagingException (although it's probably that it's looking for it because it wants to throw it, but those are issues for later).

Check if your mail.jar actually contains this class, and check if your mail.jar really is on the classpath.

The last thing that could happen is that the class is incompatible with your version of Java. Classes compiled for 1.5 won't run on 1.4, for example.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, My mail.jar contains MessagingException class.
Edited my answer to give other possibilities.
I am using the java 1.6 version and mail.jar is 1.4.1. I think this mail.jar is compatible with java1.6.
1

Working in eclipse and not in command line clearly say that there is multiple versions of mail jar present in the project.

Ex:

You have 3 Jars , Jar X, Jar Y and Jar Z.

With out you knowing JAR X might be already be bundled inside JAR Z ( in this case mail jar but some different version.

So what is the problem in having multiple version of same jar ? You have two different versions and you will not know which will be referenced in your project. (so if you are looking for some class from mail-2.jar in your project you will get class not found exception if the reference is made to mail-1.jar by class loader)

So how come its working properly on Eclipse ? In eclipse you can see there is an option for ORDER the library, these will be reference in the same order, but while running in command prompt we used to load the as lib/* which loads all to gather and we will not know which lib will get loaded first.

How to identify the culprit and fix the issue ?

Option 1 :

See docs page / user guide of the libs you are using to see what they have in them.

Option 2 :

  1. In eclipse move the JAR in question (mail.jar here) to TOP in the class path order. (your program should work now)
  2. Now bring down the order one by one to see where you get the error. (if you wish to find the jar which is causing this issue , duplicate reference)

  3. Extract the jar which has the duplicate reference remove the duplicate inside the JAR and repackage it. (if required)

Comments

0

Also be sure you're having also the java activation.jar jar.

Are you by any chance working with an App Server (eg. Jboss), if so, just check classloading model (in jboss with single classloading model you may be loading a not so up-to-date version of the jars)

5 Comments

I am not using any App Server, just running through command prompt
Do you run your test via a script or are you using some IDE (Eclipse) ? Java Mail Api AFAIK just relies on its own jar and activation framework. of course under windows user proper CLASSPATH syntax (; as a separator) and be sure you can actually read the jars. Also check you're running the proper java/javac (using java/javac -version)
from eclipse IDE its working fine. but when i am trying to run from command prompt i m getting this exception
If it works from within eclipse, but not from command prompt, then you're certainly not running it right.
from command prompt i am getting this exception, if any issues with class path, i added to mail.jar to classpath and also added in buildpath of eclipse.I m not getting why is not working
0

I've also faced the same error. It happens to me that while compiling i used the jars correctly. While running it on command line i noticed the mail.jar (javamail API) was not included.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.