On 11/10/2011 12:59, Peter Lavin wrote: > > Hi Christopher, Pid, Filippo, thanks for your help. I have got this > working as required on my Tomcat server. Here is the code that works. > > public class MyCustomClassLoader extends ClassLoader { > > public MyCustomClassLoader(ClassLoader contextClassLoader) { > super(contextClassLoader); > } > > public synchronized Class<?> arrayToClass(String name, byte[] ct, > boolean resolve) { > Class<?> c = defineClass(name, ct, 0, ct.length); > try { > this.loadClass(c.getName()); > } catch (ClassNotFoundException e) { > e.printStackTrace(); > } > > if (resolve) > System.out.println("Resolving Class in " + this.getClass()); > resolveClass(c); > > return c; > } > } // end class > > // This is called by... > MyCustomClassLoader cl = new > MyCustomClassLoader(Thread.currentThread().getContextClassLoader()); > Class<?> sp = cl.arrayToClass(null, byArray, True); > // use the now loaded class sp for something here > > > So passing the parent ClassLoader to the constructor was the key. > > I'm curious to know what the class > org.apache.catalina.loader.WebappClassLoader is intended for? > > I was trying to do... > MyCustomClassLoader extends org.apache.catalina.loader.WebappClassLoader{
It's the classloader the webapp uses. Extending it doesn't mean you inherit the relationships it holds, you still have to pass in a parent classloader in the constructor. p > and had to add tomcat-catalina-7.0.0.jar to my dependencies in Eclipse. > This works in the IDE but I get the following error when attempting to > deploy the subsequent war in Tomcat (as the jar ended up in the WAR file > too)... > > Oct 11, 2011 10:42:30 AM org.apache.tomcat.util.digester.Digester > endElement > SEVERE: End event threw exception > java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml > addServlet > > What is the class WebappClassLoader intended for? It looks like what > Christopher's question is valid :-) >> I have a question, though, since the default parent ClassLoader of >> any ClassLoader /should/ be the current thread's context ClassLoader, >> so none of the above code should be necessary. > > but the default ClassLoader used does not appear to be the one of the > Webapp in question. > > thanks again, > regards, > Peter > > > > Christopher Schultz wrote: >> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 >> >> Pid, >> >> On 10/10/2011 2:23 PM, Pid wrote: >>> On 10/10/2011 18:51, Peter Lavin wrote: >>>> Hi Filippo, tks for your reply. >>>> >>>> I'm not actually specifying any ClassLoader, perhaps I should? How >>>> should I specify a ClassLoader to use when dynamically loading a class? >>> You can pass one into the Constructor of your own classloader: >>> >>> public CustomClassloader(ClassLoader parent) { super(parent); } >> >> And to be perfectly explicit, OP should be doing this when >> constructing their CustomClassLoader (presumably in a >> ServletContextListener): >> >> CustomClassLoader cl = new >> CustomClassLoader(Thread.currentThread().getContextClassLoader()); >> >> This should allow Peter's ClassLoader to find superclass definitions >> from the webapp's ClassLoader. >> >> I have a question, though, since the default parent ClassLoader of >> any ClassLoader /should/ be the current thread's context ClassLoader, >> so none of the above code should be necessary. >> >> Perhaps the CustomClassLoader is being initialized in a weird way >> that does not make the above connection. >> >> - -chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 >> (MingW32) Comment: Using GnuPG with Mozilla - >> http://enigmail.mozdev.org/ >> >> iEYEARECAAYFAk6TWHwACgkQ9CaO5/Lv0PD//gCgthcJU6rJ5pVjkJt3AOgEHqGq >> EeYAnjq+VkKl/Ojx0QbP2lXPh5062bR6 =3mOs -----END PGP SIGNATURE----- >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For >> additional commands, e-mail: users-h...@tomcat.apache.org >> >
signature.asc
Description: OpenPGP digital signature