Problem With Dynamic Classloading In A Servlet
bwinspur Feb 24, 2004 2:04 PMI reported previously some problems with dynamically loading a class in a
servlet (I was getting the 'no classloaders' error). On a suggestion from this list
I used the current thread's classloader,
Thread.currentThread().getContextClassLoader().loadClass(fqn);
srvrObj = srvrClass.newInstance();
the problem went away, and the servlet and the dynamically instantiated object
executed as expected.
However, the class in question was trivial and used only two other classes,
java.util.logging.Logger and java.lang.String, both of which are in the platform's
rt.jar, which is presumably on the jboss jvm's boot classpath.
A more complex example, where the dynamically loaded class refers to classes
in jars stored in the war's WEB-INF/lib/ directory, fails with the following
jboss console stack trace:
10:43:15,453 ERROR [Engine] ----- Root Cause -----
java.lang.NoClassDefFoundError: com/wynnon/commandTunnel/server/examples/NokNokStateMachineAdapter
at com.wynnon.commandTunnel.server.examples.NokNokJokeRepository.(NokNokJokeRepository.java:41)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.wynnon.commandTunnel.server.CommandTunnelServlet.bindProtocolServer(CommandTunnelServlet.java:261)
at com.wynnon.commandTunnel.server.CommandTunnelServlet.doPost(CommandTunnelServlet.java:112)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
I think the trace shows that class NokNokJokeRepository has dynamically loaded OK,
but the newInstance method, has invoked a classloader (preumably the current thread's classloader
which loaded NokNokJokeRepository) that does not know about the jar files in WEB-INF/lib/.
I'm kinda stumped at this point, not yet being a hotshot on the jboss classloading architecture,
allthough I am RTFM. If anyone can suggest things I could try or look into, I'd appreciate it.
Bill.
==================================
I'm running jboss 3.2.3, in the following environment
10:38:53,265 INFO [ServerInfo] Java version: 1.4.2_02,Sun Microsystems Inc.
10:38:53,265 INFO [ServerInfo] Java VM: Java HotSpot(TM) Client VM 1.4.2_02-b03,Sun Microsystems Inc.
10:38:53,265 INFO [ServerInfo] OS-System: Windows XP 5.1,x86
====================================
Also, I'd just like to say that NokNokJokeRepository, above, is a debugging
artifact not a contract deliverable of which I have several. :) ... :(