★ wanayoo — archive 1999 http://www.webtechniques.com/sourcecode/1999/10/java/6.lstNouvelle recherche | Portail wanayoo

 
| home | current issue | archives | source code | events | demo express | search | editorial calendar | advertising | customer service | author guidelines | jobs | about

S O U R C E   C O D E / 1999 / 10 / java / 6.lst

// A Custom Class Loader

import java.io.*;
import java.util.Hashtable;

class loadme extends ClassLoader
  {
  protected Hashtable classes;
  public loadme()
    {
    classes=new Hashtable();
    }

  public static void main(String args[]) 
    {
    try
      {
      loadme loader = new loadme();
      Class x=loader.loadClass("TestClass");
      Object o=x.newInstance();
      System.out.println("Success!");
      }
    catch (Exception e)
      {
      e.printStackTrace(System.out);
      System.out.println(e);
      }
    }
  protected Class loadClass(String name, 
                            boolean resolve) throws ClassNotFoundException
    {
    Class thisclass;
    System.out.println("loadClass: "+name);
    if (!classes.containsKey(name)) 
        {
        // try original
	try
	  {
          thisclass=findSystemClass(name);
          classes.put(name,thisclass);
          }
        catch (ClassNotFoundException e)
       {
   // not a system class
       try // file with no extension in current dir?
         {
         FileInputStream fis = new FileInputStream(name);
         byte bytecodes[] = new byte[fis.available()];
         fis.read(bytecodes);
         classes.put(name,defineClass(name,bytecodes,0,bytecodes.length));
         }
        catch (IOException e1)
          {
          throw new ClassNotFoundException();
          }
         }
        }
        if (resolve)
          resolveClass((Class)classes.get(name));
        return (Class) classes.get(name);
       }

            
      }


| home | current issue | archives | source code | demo express | events | search | editorial calendar | advertising | customer service | author guidelines | jobs | about




Entire contents copyright 1996-2000 CMP Media Inc.
Read our privacy policy.