-1

This is a bit of a specific problem, but I am using javamail to connect to an IMAP server to pull some messages, and when I run my program locally in the command line it runs just fine, but when it is uploaded to tomcat to run, I get this casting error:

java.lang.ClassCastException: javax.mail.internet.MimeMultipart cannot be cast to javax.mail.internet.MimeMultipart

the line of code is:

MimeMultipart mp;
mp = ((MimeMultipart)message.getContent());

message is defined as:

Message message = null;
message = folder.getMessageByUID(14299);

Im not sure why I can cast it when it is run locally, but it crashes on the server. I am using the same message both times, it is hard coded, so there is no chance I will be pulling a message that is not a MimeMultipart. Any ideas? If you need me to post anything else, I will, just ask. Thanks!

Wingdom
  • 439
  • 2
  • 10
  • 23

3 Answers3

0

This is probably a Classloader issue, see this question for several possible solutions:

Community
  • 1
  • 1
Sirs
  • 1,277
  • 17
  • 30
0

Based on the information available to us in the current state of the question, the best way to arrive at your solution is to verify the ClassLoader that loaded MimeMultipart:

ClassLoader myClassLoader = new MyClass().getClass().getClassLoader();

However, going down that path could take you longer than expected.

There's a good chance that this is a localized error on your Tomcat installation. If you're able to try it on a different server like GlassFish, or perhaps a fresh install of Tomcat, you may find the problem resolved. You've already confirmed that your program is fine via command line.

Jops
  • 22,535
  • 13
  • 46
  • 63
0

An error message of the type :

java.lang.ClassCastException: CLASS cannot be cast to CLASS

often indicates a classloader issue. You are trying from one ClassLoader to cast to an instance which resides in another ClassLoader.

I am guessing that the javamail jar are in the /WEB-INF/lib folder of your webapp. In that case, if they are also in the lib folder of tomcat, you will have that exact message.

See this link for more information on tomcat ClassLoading.

One way to fix this is to delete the javamail libs from your tomcat installation and leave them in the webapp, or, to not includes them in the war.

Cédric Couralet
  • 4,913
  • 1
  • 21
  • 21