Results 1 to 13 of 13

Thread: Creating a ClassLoader?

Hybrid View

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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;
      } 
     }
    Last edited by Dilenger4; Oct 12th, 2003 at 03:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width