<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Problems with the tomcat classloader
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/roli82001">Roland Käser</a> in <i>Javassist</i> - <a href="http://community.jboss.org/message/639418#639418">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>While developing a very small and lightweight j2ee linke server, I try to extend a abstract class at runtime using: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>ClassPool pool = ClassPool.getDefault();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pool.insertClassPath(new ClassClassPath(this.getClass()));</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CtClass cc = pool.makeClass("HelloWorldClientBean"); </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cc.setSuperclass(pool.get("ch.isn.dante.comserver.test.HelloWorldSessionBean"));</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Class beanClass = cc.toClass();</p><p>cc.defrost();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Constructor constructor = beanClass.getConstructor(argsClass);</p><p>&#160; </p><p>Object[] params = new Object[]{new ComServerSession(),null};&#160;&#160; </p><p>HelloWorldSessionBean sessionBean = (HelloWorldSessionBean)constructor.newInstance(params); </p><p>System.out.println("Hello World Output: " + sessionBean.getHelloWorld());</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>This works fine on the client side (regular java vm). </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>On the server side I get first a very strange exception which was stangely related to the tomcats classloader. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of&#160; org/apache/catalina/loader/WebappClassLoader): attempted&#160; duplicate class definition for name: "ch/isn/dante/comserver/test/HelloWorldSessionBeanExecutable"</p><p>javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of&#160; org/apache/catalina/loader/WebappClassLoader): attempted&#160; duplicate class definition for name: "ch/isn/dante/comserver/test/HelloWorldSessionBeanExecutable"</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javassist.ClassPool.toClass(ClassPool.java:1089)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javassist.ClassPool.toClass(ClassPool.java:1032)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javassist.ClassPool.toClass(ClassPool.java:990)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javassist.CtClass.toClass(CtClass.java:1125)</p><p>...</p><p>Caused by: java.lang.LinkageError: loader (instance of&#160; org/apache/catalina/loader/WebappClassLoader): attempted&#160; duplicate class definition for name: "ch/isn/dante/comserver/test/HelloWorldSessionBeanExecutable"</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.ClassLoader.defineClass1(Native Method)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.ClassLoader.defineClass(ClassLoader.java:615)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.ClassLoader.defineClass(ClassLoader.java:465)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.reflect.Method.invoke(Method.java:597)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javassist.ClassPool.toClass2(ClassPool.java:1102)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javassist.ClassPool.toClass(ClassPool.java:1083)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>To prevent this I wrote a very ugly hack to use the system class loader and add the classpath in which this superclass:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><table><tbody><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";">URL url = new File(serverServlet.getInstallDir() + "WEB-INF/classes/").toURL();</td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";">URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();</td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";"> Class sysclass = URLClassLoader.class;</td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";"> Method method = sysclass.getDeclaredMethod("addURL", parameters);</td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";"> method.setAccessible(true);</td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td><td style=";"> method.invoke(sysloader, new Object[]{url});</td></tr></tbody></table><p> and created the class using this modified classloader:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><table><tbody><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";">Class beanClass = cc.toClass(sysloader);&#160; </td><td style=";"><br/></td><td style=";"><br/></td><td style=";"><br/></td></tr><tr><td style=";"><br/></td><td style=";"><br/></td><td style=";">cc.defrost();</td></tr></tbody></table><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now I get a NoSuchMethodException:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>java.lang.NoSuchMethodException: HelloWorldServerBean.&lt;init&gt;(ch.isn.dante.comserver.server.ComServerSession, ch.isn.dante.comserver.server.ServerApplication)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.Class.getConstructor0(Class.java:2706)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at java.lang.Class.getConstructor(Class.java:1657)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at ch.isn.dante.comserver.server.ComServerSession.loadClass(ComServerSession.java:239)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at ch.isn.dante.comserver.server.ComServer.doGet(ComServer.java:286)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at ch.isn.dante.comserver.server.ComServer.doPost(ComServer.java:417)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I can't see from which this error comes. When I&#160; inspect the newly created class I get one constructor:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public HelloWorldClientBean(ch.isn.dante.comserver.server.ComServerSession,ch.isn.dante.comserver.server.ServerApplication)</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/639418#639418">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Javassist at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>