3 Replies Latest reply on Apr 13, 2004 12:34 PM by Nicholas Whitehead

    Loading classes via reflection and NoClassDefFoundError

    dlamotta Newbie

      Hello all. I seem to be having trouble with classes loaded via Reflection and I was wondering if anybody in the forum can provide some pointers. The problem is as follows:

      I have class files RedApple.class, GreenApple.class, YellowApple.class sitting on the filesystem where JBoss 4.0 DR3 is running. For all intents and purposes lets say those 3 class files are in /tmp on a Linux machine running Java 1.4.2_03.

      The colored apple classes all extend Apple. Apple, in contrast with the colored apples, lives in a Jar file that has been placed along the FruitBean in the $JBOSS/server/fruits/deploy directory. A helper class to FruitBean (say, FruitHarvester, which is a custom class loader) instantiates colored apples in the following manner:

      1. Reads from a properties file where the colored apple class files are located.
      2. Via an input stream it loads a byte array for the appropriate colored apple we are about to harvest.
      3. FruitHarvester issues a call to ClassLoader.defineClass and passes it the name of the colored apple, the bytes it read from the file, 0 and the length of the byte array.

      If you trace ClassLoader.defineClass you'll see that it makes a call to defineClass0() which is a native call. That is the last call in my stack trace and the exception I get is a NoClassDefFoundError on Apple.

      I have run out of ideas why Apple cannot be seen by JBoss, especially because Apple is in the Jar file I deployed. I have tried putting that same Jar file in the $JBOSS/server/fruits/lib directory, I have tried extracting Apple.class and putting it in that same dir, I have tried putting the Jar file in $JBOSS/lib directly, and none of these things helps JBoss find the Apple class. The properties file is read without a problem and the bytes for the class file are successfully read, the problem is when loading the class. It is as if JBoss has half of what it needs (colored apple class) but it's missing the other half (Apple), even though the later is in the lib directory in a Jar file.

      Please, if anybody has any pointers do let me know. I have ran out of ideas on this problem.


      // David

        • 1. Re: Loading classes via reflection and NoClassDefFoundError
          Joachim Van der Auwera Expert

          How are you loading your classes?
          Are you using Class.forName ?

          If you are, then that may be the cause of your problems. In JBoss you should not use the system classloader, as that one does not handle the hot deployment, the UnifiedClassloaders et al. You should use


          • 2. Re: Loading classes via reflection and NoClassDefFoundError
            dlamotta Newbie

            No, I am not using Class.forName. I am using ClassLoader.defineName. I believe the problem stems from the ClassLoader. Although it does not solve my original problem, using Thread.currentThread().getContextClassLoader().loadClass() alleviates the problem a little bit. For this, I have to put colored apples in a Jar and then drop that Jar in the lib directory of my server.

            Again, if I stick to my original implementation (i.e., reading class files from the filesystem, populating byte arrays, and then calling defineClass()) I am still hosed.

            Thanks for your suggestion, though.

            • 3. Re: Loading classes via reflection and NoClassDefFoundError
              Nicholas Whitehead Novice


              You need to delegate the class loading to the cloass loader for the deployment that FruitHarvester was loaded by. The NoClassDefFoundError error is caused by same classes being loaded by two seperate class loaders.

              Make your custom class loader delegate to: