Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting javax.mail.MessagingException

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 ...

like image 311
ramesh Avatar asked Nov 05 '22 15:11

ramesh


2 Answers

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.

like image 126
Joeri Hendrickx Avatar answered Nov 12 '22 11:11

Joeri Hendrickx


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)

like image 44
Karthikeyan Rajendran Avatar answered Nov 12 '22 11:11

Karthikeyan Rajendran