0 Replies Latest reply on Aug 15, 2007 11:24 AM by Alex Krasov

    Inserting custom classloader to delegation chain?

    Alex Krasov Newbie

      Hello.

      I'm trying to define a custom classloader to create certain classes at runtime.

      My classloader itself is just deriving from ClassLoadder and overriding the findClass with something like

      @Override
       protected Class<?> findClass(String className) throws ClassNotFoundException {
       ClassLoader parentLoader = this.getClass().getClassLoader();
       if(className.equals("classToBeCreatedAtRuntime")){
       Class c = createClass(className);
       return c;
       }else{
       return Class.forName(className, true, parentLoader);
       }
       }
      


      What I want from my classloader is to create and load class at runtime if needed or just delegate the responsibility to parent class loader.

      The main problem I face is to "push" my custom loader into the delegation chain. I've tried to set it with Thread.currentThread().setContextClassLoader but I'm getting classcast exceptions, so this doesn't seems like a right way.

      Am I missing something? How this can be achieved?

      Thanks a lot.