I’ve written a custom MBean that deploys in Tomcat 6. One of its tasks is to query a database value. I’m doing this by loading up the database resource using JNDI – the resource is defined in Tomcat’s server.xml.
The problem is that when I create an instance of javax.naming.InitialContext it throws a ClassNotFoundException as it can’t find org.apache.naming.java.javaURLContextFactory.
This class is in catalina.jar and loaded by the common classloader. The jar containing my MBean code is loaded by a shared classloader.
Any ideas as to how I can get around this?
To note: my MBean is loaded by a ContextListener which I’ve defined in the tomcat/conf/web.xml. I’ve also defined it in a webapp web.xml which makes no difference. I can’t really move my jar so as to be loaded by the common classloader as it relies on classes loaded by the shared classloader.
Thanks in advance,
Will
It looks a weird classloading or security/permission issue. Below is a workaround.
The main idea: Since the
ServletContextListenercould call thenew InitialContext()without theClassNotFoundExceptionget it in the listener and pass it to the constructor of the MBean object before you register the MBean. I used a simple web application and I have not modifiedtomcat/conf/web.xml.Resource configuration in the
tomcat/conf/context.xml:The
web.xmlresource configuration:The
ServletContextListenerwhich registers the MBean:MBean interface:
MBean implementation:
The MBean Descriptor How To says a
mbeans-descriptor.xmlis required but it’s worked without it well. I could connect to the HelloMBean withjconsole. CallingsayHello()throughjconsoleprinted the following:Sources: