This is pretty much what they have. So it seems that the loadClass() method is overridden.
What i dont understand is the check to see if the class is already loaded in the loadClass() method. If findClass() cannot find the class then a ClassNotFoundException is thrown but then the rest of the code in the loadClass() method is skipped.
Code:public class Y{ public static void main(String[] args){ try{ Class c; Loader l = new Loader(); c = l.loadClass("C:\\Java\\E.class"); }catch(ClassNotFoundException e){System.err.println(e);} } }Code:import java.io.*; public class Loader extends ClassLoader{ protected Class findClass(String classname) throws ClassNotFoundException{ byte[] classbytes = null; try{ Scanner s = new Scanner(classname); classbytes = s.getByteCode(); }catch(IOException e){System.err.println(e); } if(classbytes != null){ return defineClass(classname, classbytes,0,classbytes.length); } throw new ClassNotFoundException(classname); } public Class loadClass(String name) throws ClassNotFoundException{ // check if the class is already loaded Class loadedclass = findClass(name); if(loadedclass == null){ // search for class in local repository before delegating //......... // if class not found delegate to parent loadedclass = this.getClass().getClassLoader().loadClass(name); } return loadedclass; } }




Reply With Quote