Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:01:14+00:00 2026-05-25T23:01:14+00:00

I’ve written a custom MBean that deploys in Tomcat 6. One of its tasks

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:01:14+00:00Added an answer on May 25, 2026 at 11:01 pm

    It looks a weird classloading or security/permission issue. Below is a workaround.

    The main idea: Since the ServletContextListener could call the new InitialContext() without the ClassNotFoundException get 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 modified tomcat/conf/web.xml.

    Resource configuration in the tomcat/conf/context.xml:

    <Context>
    ...
        <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
            maxActive="100" maxIdle="30" maxWait="10000"
            username="root" password="..." driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
    ...
    <Context>
    

    The web.xml resource configuration:

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

    The ServletContextListener which registers the MBean:

    import java.lang.management.ManagementFactory;
    
    import javax.management.MBeanServer;
    import javax.management.ObjectName;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class ContextListener implements ServletContextListener {
    
        private ObjectName objectName;
    
        public void contextInitialized(final ServletContextEvent sce) {
            System.out.println("---> bean context listener started");
    
            final MBeanServer mbeanServer = 
                ManagementFactory.getPlatformMBeanServer();
            try {
                final InitialContext initialContext = new InitialContext();
                final Context envContext = 
                    (Context) initialContext.lookup("java:/comp/env");
    
                objectName = new ObjectName("com.example:type=Hello");
                final Hello helloMbean = new Hello(envContext);
                mbeanServer.registerMBean(helloMbean, objectName);
                System.out.println("---> registerMBean ok");
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    
        public void contextDestroyed(final ServletContextEvent sce) {
            System.out.println("---> bean context listener destroyed");
            final MBeanServer mbeanServer = 
                ManagementFactory.getPlatformMBeanServer();
            try {
                mbeanServer.unregisterMBean(objectName);
                System.out.println("---> unregisterMBean ok");
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    MBean interface:

    public interface HelloMBean {
        void sayHello();
    }
    

    MBean implementation:

    import java.sql.Connection;
    
    import javax.naming.Context;
    import javax.sql.DataSource;
    
    public class Hello implements HelloMBean {
    
        private final Context envContext;
    
        public Hello(final Context envContext) {
            this.envContext = envContext;
            System.out.println("new hello " + envContext);
        }
    
        @Override
        public void sayHello() {
            System.out.println("sayHello()");
    
            try {
                final DataSource ds = 
                    (DataSource) envContext.lookup("jdbc/TestDB");
                final Connection conn = ds.getConnection();
                System.out.println("   conn: " + conn);
                // more JDBC code
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    The MBean Descriptor How To says a mbeans-descriptor.xml is required but it’s worked without it well. I could connect to the HelloMBean with jconsole. Calling sayHello() through jconsole printed the following:

    conn: jdbc:mysql://localhost:3306/javatest?autoReconnect=true, \
    UserName=root@localhost, MySQL-AB JDBC Driver
    

    Sources:

    • MBeans and Tomcat: A HOWTO Guide for Managing YOUR Application
    • JNDI Datasource HOW-TO
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.