title: Classloaders and sharing .jar files with Apache Tomcat
body: <p>If I have classes that need to be shared between my webapp and Tomcat (e.g. a custom realm and principal), where should the .jar file containing those classes go?</p>
  
  <p>Currently I'm putting the .jar in ${CATALINA_HOME}/lib.  This result is a ClassCastException when assigning references from classes of the same type.  Here's an example:</p>
  
  <pre><code>MyCustomPrincipal principal = (MyCustomPrincipal)FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
  </code></pre>
  
  <p>The method above throws a ClassCastException.  The method returns an actual MyCustomPrincipal type (since that's what my custom realm gave Tomcat when it performed authentication) that, apparently, was created by a different classloader.  How do I fix this so both Tomcat and my webapp can use MyCustomPrincipal?</p>
  
  <p><a href="http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html" rel="nofollow noreferrer">http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html</a></p>
  
  <p>Any help is appreciated.
  Andrew</p>