2 Replies Latest reply on Sep 17, 2008 4:08 AM by tuezney

    dynamically load classes within app on JBOSS

    tuezney

      Hello,

      I am using jboss 4.2.3.GA and am trying to load a class dynamically as shown in your example on :

      http://wiki.jboss.org/wiki/HowCanIDynamicallyLoadClassesWithinAnMBean


      this is a piece of my code :

      // Load the class :
      logger.debug("getting classLoader ");
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      logger.debug("Loading class " + className);
      Class deployableApplicationClass = classLoader.loadClass(className);
      logger.debug("Instantiating Object");
      Object deployableApplicationObject = deployableApplicationClass.newInstance();



      // implemented interfaces
      logger.debug("listing interfaces: ");
      for (Class clt : deployableApplicationObject.getClass().getInterfaces()){
      logger.debug("list interfaces deployableApplicationObject implements: " + clt.getName());
      }

      logger.debug("casting");
      app = (DeployableApplication)deployableApplicationObject;


      It always fails on the cast : app = (DeployableApplication)deployableApplicationObject;

      with a ClassCastException

      this is the server log :

      2008-09-15 12:52:39,932 DEBUG [be.kmi_irm.labo.web.LoadAppsController] getting classLoader
      2008-09-15 12:52:39,932 DEBUG [be.kmi_irm.labo.web.LoadAppsController] Loading class be.kmi_irm.labo.imaging.medriasclient.MedriasClient
      2008-09-15 12:52:39,946 DEBUG [be.kmi_irm.labo.web.LoadAppsController]
      Instantiating Object
      2008-09-15 12:52:39,979 DEBUG [be.kmi_irm.labo.web.LoadAppsController] listing interfaces:
      2008-09-15 12:52:39,980 DEBUG [be.kmi_irm.labo.web.LoadAppsController] list interfaces deployableApplicationObject implements: be.kmi_irm.labo.web.app.DeployableApplication
      2008-09-15 12:52:39,980 DEBUG [be.kmi_irm.labo.web.LoadAppsController] casting
      2008-09-15 12:52:39,980 DEBUG [be.kmi_irm.labo.web.LoadAppsController] ClassCastException : be.kmi_irm.labo.imaging.medriasclient.MedriasClient cannot be cast to be.kmi_irm.labo.web.app.DeployableApplication
      2008-09-15 12:52:39,981 ERROR [STDERR] java.lang.ClassCastException: be.kmi_irm.labo.imaging.medriasclient.MedriasClient cannot be cast to be.kmi_irm.labo.web.app.DeployableApplication



      While the loaded class implements the interface "DeployableApplication"


      Could you tell me what is going wrong.


      Kind regards,

      Alain

        • 1. Re: dynamically load classes within app on JBOSS
          crankit

          Alain, what is app defined as? What is its type?

          • 2. Re: dynamically load classes within app on JBOSS
            tuezney

            The app on jboss is a webapp, it is a servlet that tries to classload a jar file with a POJO app that is in the deploy directory.

            This worked in our previous jboss version (JBoss_4_0_1_SP1) for e few years. We used
            a bea jrockit 1.5 jvm.

            Now I am upgrading our server to 4.2.3.GA and our java version to sun's 1.6 imp.

            The whole idea is the following.
            Use all the nice stuff of the appserver :
            - db connection pooled and managed by container
            - 1 jvm for all applications
            - unified classloading
            - jmx
            - jms

            Without the use of ejb3, writing a deployment descriptors, annotations all over the place etc etc etc

            And still have the possibility (if needed) to run the applications (for test eg) without the appserver.

            We succeeded in this by developping servlet and an interface (DeployableApplication).

            All the developer has to do is to implement the DeployableApplication, pack this into a jar file and deploy it in the deploy directory. The app will be started with a request on the servlet I mentioned before.