3 Replies Latest reply on Oct 21, 2002 4:35 PM by javajohnson

    ClassLoaders drive me nuts!

      I am using JBoss 3.0.3

      I have a session bean called 'SecurityFactory' that is supposed to return an instance of an class derived from an abstract class of mine, SecurityKey (I am leaving all the packages off of the names for this post for clarity)

      The method needs to dynamically instantiate a class given the fully qualified class name.

      I have a default implementation of a SecurityKey derived class, called SecurityKeyImpl.

      My problem is that I cannot get the class loaded via any way besides doing 'new SecurityKeyImpl()' Obviously, that won't work for dynamic class loading.

      I have tried the following:

      Class.forName("...SecurityKeyImpl");

      Class.forName("...SecurityKeyImpl",true,Thread.currentThread().getContextClassLoader());

      Class.forName("...SecurityKeyImpl",true,this.getClass().getClassLoader());

      Thread.currentThread().getContextClassLoader().loadClass("...SecurityKeyImpl");

      this.getClass().getClassLoader().loadClass("...SecurityKeyImpl");



      The SecurityFactory EJB is in a jar called Security.jar, the jar contains the remote, home, impl of the bean, as well as the SecurityKey abstract class and the SecurityKeyImpl class.

      To make things even more confusing (to me) I tried

      Object o = new SecurityKeyImpl();
      ClassLoader cl = o.getClass().getClassLoader()

      When I then printed out the cl object it was of type org.jboss.mx.loading.UnifiedClassLoader

      and if I did a Class.forName() call after that I could load the class. However, if I commented out the instantiation above, JBoss could no longer find the class.


      Sorry, I know this is long, but I appreciate any help from someone who understands the ClassLoading logic (logic???)

      Thanks,

      Mark

        • 1. Re: ClassLoaders drive me nuts!

          What do you get for the following output?
          If should include the url for classes' location.

          System.out.println(getClass().getClassLoader());
          Object o = new SecurityKeyImpl();
          System.out.println(o.getClass().getClassLoader());

          Regards,
          Adrian

          • 2. Re: ClassLoaders drive me nuts!

            I placed the code you provided into my code and get the following: (the formatting is awful, sorry)

            13:11:45,031 INFO [STDOUT] org.jboss.mx.loading.UnifiedClassLoader@7cd15d{ url=
            file:/C:/jboss-3.0.3/server/default/tmp/deploy/server/default/deploy/TNTDIR.ear/Security.jar/65.Security.jar }
            13:11:45,031 INFO [STDOUT] org.jboss.mx.loading.UnifiedClassLoader@7cd15d{ url=
            file:/C:/jboss-3.0.3/server/default/tmp/deploy/server/default/deploy/TNTDIR.ear/Security.jar/65.Security.jar }


            My file exists in Security.jar

            I am doing this with an exploaded EAR file. But it shouldn't matter, right? My ear file is exploaded, but the EJB jar files are not. I only exploaded the .EAR and my .WAR files.

            Thank you for any assistance.

            --Mark

            • 3. Re: ClassLoaders drive me nuts!

              Hmm....

              This is getting even stranger. I decided to explicitly state my classpath in the manifest file.

              The printout of the ClassLoader was:

              15:41:57,202 INFO [STDOUT] org.jboss.mx.loading.UnifiedClassLoader@41b635{ url=file:/C:/jboss-3.0.3/server/default/tmp/deploy/server/default/deploy/TNTDIR.ear/Security.jar/70.Security.jar }

              It was essentially the same. However, this time it works. I pointed the manifest classpath to a jar file that contains every single one of the .class files in my application.

              I verified that this was what was going on by deleting the manifest file, and it went back to the old behavior.

              But, now that I have it working, I want to know why.

              1. Why does the printout for the UnifiedClassLoader show the same classpath regardless of the manifest file, yet it finds classes in different places?
              2. What should the classpath be in a .mf file?

              Thanks agian,

              Mark J.