import java.util.Hashtable; import java.net.*; import java.io.*; public class URLClassLoader extends ClassLoader { Hashtable cache; URL u; public URLClassLoader(URL u) { this.u=u; cache = new Hashtable(); } public synchronized Class loadClass(String name, boolean reslove) throws ClassNotFoundException { Class c = (Class) cache.get(name); if(c==null) { try { c =findSystemClass(name); } catch (ClassNotFoundException e) {} } if(c==null) { byte b[] = loadClassData(name); c = defineClass(b,0,b.length); cache.put(name,c); } System.out.println("now resolve"); if(reslove) { System.out.println("resolve class "+c.getName()); resolveClass(c); } System.out.println("done"); return c; } private getPackageContents(String packageName){ String baseURL } private byte[] loadClassData(String name) throws ClassNotFoundException { byte[] b; InputStream theClass = null; int bfr =128; try { URL classURL = new URL(u, name+".class"); URLConnection uc = classURL.openConnection(); uc.setAllowUserInteraction(false); try { theClass = uc.getInputStream(); } catch (NullPointerException e) { System.out.println(e); throw new ClassNotFoundException(name+" input stream problem"); } int cl = uc.getContentLength(); if(cl==-1) { b = new byte[bfr*30];} else { b = new byte[cl+bfr];} System.out.println("0"); int bytesread =0; int offset =0; while (bytesread >= 0) { bytesread = theClass.read(b,offset,bfr); if(bytesread == -1) break; offset += bytesread; System.out.println(offset+" of "+b.length+" of "+cl); if(cl ==-1 && offset == b.length){ byte temp[] = new byte[offset*2]; System.arraycopy(b,0,temp,0,offset); b=temp; } else if(offset > b.length) { throw new ClassNotFoundException(name + " error reading data into array"); } } System.out.println("2"); if(offset < b.length) { System.out.println("arraycopy"); byte temp[] = new byte[offset]; System.arraycopy(b,0,temp,0,offset); b=temp; } if(cl != -1 && offset != cl) { throw new ClassNotFoundException("Only "+offset+" bytes received for " +name +"\n Expected "+cl+" bytes"); } System.out.println("done?"); } catch (Exception e) { throw new ClassNotFoundException(name + " "+e); } finally { try { if(theClass != null) theClass.close(); } catch (Exception e) {} } System.out.println("return"); return b; } }