java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
java, tomcat
Solution
This problem occurs in the server because the lib of Tomcat and you application both have their own copy of `mail.jar` (in WEB-INF/lib), so the classloaders can load two different Session.If you delete the `mail.jar`from your application, this problem will be solved.
Problem
I have been receiving this error when trying to run the following code to get a javax.mail.Session object using a tomcat context file. ``` Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); Session session = (javax.mail.Session) envCtx.lookup("mail/session"); ``` This is the resource declaration in the context.xml. ``` <Resource name="mail/Session" auth="Container" type="javax.mail.Session" mail.smtp.host="host" mail.smtp.user="user" mail.smtp.password="password" mail.smtp.auth="false"/> ``` I understand this can be due to me having the same library for the javax.mail.Session in my application server library(tomcat) folder and in my applications library folder, i have eliminated as many duplicate library files from my application library folder (e.g. mail.jar) that i can see have the javax.mail.Session as part of the library, now i am at a point where i am a still getting this error and not sure what other libraries could be the source of this problem, or is it some other issue that i am not aware of? What would people suggest i do to find the source of this problem? Thanks.