1

I wrote custom class loader which loads jar file from the file system. the customClassLoader override the load and the find method and its working by How can I make all the code after the definition of the class loader to work with the customClassLoader in context of the method execute. Once I run this code in the method f1() I get this error java.lang.NoClassDefFoundError org.xml.dd.myclass

How can I define that in context of the method execute all the time I will work with customClassLoader

Public void execute()
{

ClassLoader customClassLoader= new customClassLoader();
        try
        {
            Class.forName("org.xml.dd.myclass", true, xdmCustomClassLoader);
        }
        catch (ClassNotFoundException e2)
        {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } 
        Thread.currentThread().setContextClassLoader(customClassLoader);
        ………………….
        F1();
        F2();       
}
user1205079
  • 395
  • 1
  • 5
  • 20
  • Check out this post http://stackoverflow.com/questions/10192453/java-classloader-change/10192896#10192896 – mazaneicha Apr 18 '12 at 13:13
  • This post is talking about invoke methods in the declared after found it, the issue that i can;t use newInstance() method because its an interface and i don;t want to invoke method, i just want to make sure that the code inside method f1() and f2() uses the custom class loader – user1205079 Apr 18 '12 at 13:29

1 Answers1

2

The context classloader must be used explicitly. The normal new operations and such will use the classloader of the class that owns the code in question. In the example below, Executor is the class that will be the entry point to all the code you want executed with your custom classloader in charge. Load that class using your classloader and invoke its method run. You should implement run so it executes all the code that needs to run with your classloader in charge.

public class Executor {
  public void run() {
     final MyInterface x = new MyClass();
     x.f1(); x.f2();
  }
}

public class Test {
  public static void main(char[] args) throws Exception {
    final ClassLoader customCl = new customClassLoader();
    final Executor e = 
      (Executor) Class.forName("Executor", true, customCl).newInstance();
    e.run();
  }
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • How can i make newInstance to class who is interface i got initiatExpetion. can you please provide small example how to do this with the above code. Thanks – user1205079 Apr 18 '12 at 14:20
  • Thanks for reply its not working because class.forName must get the name of my class org.xml.dd.my.class else it will not find my class. and i dont want to invoke method on my class it;s class related to run time of third party so i just want to load it and let the code after to work with this custom class path. why Thread.currentThread().setContextClassLoader(customClassLoader); is not working. – user1205079 Apr 18 '12 at 15:25
  • You're not getting the point, I'd say. I am giving you the idiom how this is done, not solving your immediate problem. You're gonna have to rework your code until you get it into this form: one class containing a method that is the entry point to all the code executed in the context of your classloader. – Marko Topolnik Apr 18 '12 at 16:10