The best way to solve ClassLoader issues is to reduce their likelihood by following good deployment practices:
Know the basics of ClassLoaders and how your particular deployment environment uses them. Even though it sounds boring, make a deployment plan and stick with it. Know the dependencies for each J2EE module. Then factor out the common classes and utility JARs into a Common JAR that resides in the EAR. When you add new dependency classes or third party utility JARs, update the plan and refactor. Planning where your classes and utilities reside reduces the chance that you'll have ClassNotFoundException and NoClassDefFoundError issues.
Make sure that there are no duplicate classes or JAR files. Third party libraries typically cause this problem because they each may have their own copy of a particular JAR file. For example, many Apache libraries have their own copy of files such as Log4J and Commons Lang. Make sure there is only one copy of these classes or JAR files across the entire EAR deployment. This process is tedious and requires upgrading Ant build scripts, but it saves serious headaches later on. Without duplicate classes or JARs, you're less likely to see problems like the ClassCastException.
Encapsulate/Minimize Visibility to avoid ClassCastExceptions.
Put web-specific classes in the WAR file's WEB-INF/classes directory and web-specific third party JARs (like Struts, for example) in the WAR file's WEB-INF/lib directory. There's no need to put web-specific classes and JARs in the Common JAR because other J2EE modules don't depend on them.
Put extra EJB-specific utility classes and dependent JAR files in EJB JAR's root directory because the web components don't need to access them.
Use a Common JAR that contains all utility classes and JARs used in both the EJB JAR and the WAR.
Use the correct ClassLoader to avoid the ClassNotFoundException and NoClassDefFoundError. Use the Thread Context ClassLoader (Thread.currentThread( ).getContextClassLoader( )). If you use Class.forName( ), pass in the Thread Context ClassLoader as a parameter. To load a Properties file in your application's CLASSPATH, use the Thread Context ClassLoader to get an InputStream for loading the Properties file.