How can I create and install a custom classloader such that if a class cannot be found using the normal classloaders that my classloader will be invoked? I.e., I want to create a sort of last chance, failover classloader.

Luigi Viggiano

I think you cannot replace (or install) your custom classLoader. But you can use your classLoader when the system one doesn't find your class in this way:

Class myClass = null;
  try {
      myClass = ... //code that tryes to load a class
  } catch (ClassNotFoundException cnfEx) { 
      myClass = myClassLoader.loadClass();
  }

Otherwise you can define your custom ClassLoader specifying the System classLoader as parent of the your one. And defining the behaviour of the your to try before with the system one, then to try by itself to load the class.

MyClassLoader myClassLoader = new MyClassLoader(ClassLoader.getSystemClassLoader());
  Class myClass = null;
  try {
      myClass = myClassLoader.loadClass();
  } catch (ClassNotFoundException cnfEx) {
      System.err.println("Class cannot be loaded.");
  }
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact