Results 1 to 13 of 13

Thread: Creating a ClassLoader?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Creating a ClassLoader?

    This is an excerpt from an interesting artice i found inside a recent Java Developers Journal( Aug 2003 Vol:9 Issue:8) magazine.

    The loadClass(String classname) method invokes the
    findClass(String classname) method. Custom ClassLoaders
    shoud override this method to provide a custom way of
    locating and loading a Java class file.
    Ok sounds easy enough but i have a few questions. Am i doing this right? How can i scope the size of the byte array to the number of bytes within the file? Once the class is returned from the classloader how can i execute it? More questions to come.........
    Code:
    import java.io.*;
    
     public class Loader extends ClassLoader{ 
     
      private byte[] b;  
       
    
      public Class findClass(String c) throws ClassNotFoundException{
        try{
         Scanner s = new Scanner(c); 
         this.b = s.getByteCode();
        }catch(IOException e){;} 
         return b.getClass();
      }
     }
    Code:
     public class Scanner{
      
     private File res; 
      
     public Scanner(String r){ 
      res = new File(r); 
    
     }
     public byte[] getByteCode() throws IOException{
      BufferedInputStream buff; 
       buff = new BufferedInputStream(new FileInputStream(res));
       byte[] b = new byte[5000];
         long bytesread = 0;  
       while(true){
         bytesread = buff.read(b); 
         if(bytesread < 0) break;
        
       }  
        return b; 
      }
     }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Doesn't the File class have a getLength method or some such?

    I think I'll experiment with this a bit too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    byte[] b = new byte[res.length()]; compiles fine. Now im in the process of trying to figure out how to execute the class that is returned by the claassloader. I lost my Java docs. Any ideas? Thanks.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Class.createInstance?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    From what they are saying the loadClass method invokes the find class method. The find class method searches for the given class in the repository and if found reads and returns the byte codes for the class. The raw byte codes are passes to the defineClass() method implemented in the java.lang.ClassLoader Class which returns an instance of the java.lang.Class object.

    The chain of events is confusing to me. Do i have to implement all of the methods within the java.lang.ClassLoader Class? Then invoke the findClass() method from the loadClass() method and the defineClass() method from the findClass() method? It makes sense since each one returns a Class object. It sounds like it shoud just be a chain of events emanating from the source. ie. (loadClass());

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Seems quite straightforward to me. You have a CustomClassLoader that extends ClassLoader. To get a Class object you call loadClass on your CustomClassLoader. You don't override this method. The default implementation will then:
    1) Check if the class already is loaded. If yes, return class object.
    2) Call getParent().loadClass() to delegate the call to other class loaders.
    3) If there still is no match, call findClass.
    findClass will be overloaded by you. It somehow retrieves a byte array that contains the class bytecode and passes this on to defineClass, which returns a Class object, which findClass passes on to loadClass, which returns it.

    You then have a Class object and can do whatever you want with it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    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.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't think you should override loadClass. The docs explicitly say "Subclasses of ClassLoader are encouraged to override findClass(String), rather than this method."
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yes i have to agree with you on that one. Everything i have read so far about ClassLoaders has said to just override the findClass() method and keep the default implementation of the loadClass().
    method.

    Here is what i have so far and it seems to work but only if the class being loaded is in the -classpath dir. If i take the class out of that dir and put in say C:\ a java.lang.ClassNotFoundException is thrown.

    So would be correct to say that since the .class file is in dir specified in the -classpath, the system classloader is loading the class thus not executing the findClass() method?
    Code:
      import java.io.*;
    
      public class Y{
       public static void main(String[] args){
       try{
       Class c;
       Loader l = new Loader();
       c = l.loadClass("E");
    
       String classname = c.getName(); 
       System.out.println(classname);
    
       Object o = c.newInstance();
    
       if(o instanceof E){
       E e = (E)o; 
       e.printFName(); 
      }
    
      }catch(ClassNotFoundException e){System.err.println(e);}
       catch(java.lang.InstantiationException ie){System.err.println(ie);}
       catch(java.lang.IllegalAccessException iae){System.err.println(iae);}
      }
     }
    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);
      }
     }

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sounds plausible, but you can check that with a debugger.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Im using a command line compiler and notepad.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    So what, I've used the command line debugger before.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Never used it.

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