public class Loader extends ClassLoader{
    private String dir;
    public Loader(String dir)
    {
        this.dir=dir;
    }
    @Override
    protected Class findClass(String name)throws ClassNotFoundException
    {
        Class c=findLoadedClass(name);

    if(null!=c)
    {
        return c;
    }else
    {
        ClassLoader parent=this.getParent();
        try {
        c=parent.loadClass(name);
        }catch(Exception e)
        {
            //e.printStackTrace();
        }
        if(c!=null)
        {
            return c;
        }else
        {
            byte[] data=getData(name);

            if(data==null)
            {
                throw new ClassNotFoundException();
            }else
            {
                c=defineClass(name,data,0,data.length);
                System.out.println("yes");
            }
        }
    }
    return c;
}

private byte[] getData(String classname)
{
    String path=dir+"/"+classname.replace('.', '/')+".class";

    InputStream is=null;
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    try {
        URL url =new URL(path);
        is=url.openStream();
        int len=-1;
        byte[] buffer=new byte[1024];
        while((len=is.read(buffer))!=-1)
        {
            bos.write(buffer,0,len);
        }
        bos.flush();
        return bos.toByteArray();
    }catch(IOException e)
    {
        e.printStackTrace();
        return null;
    }finally {
        try {
            if(null!=is)
            {
                is.close();
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        try {
            if(null!=bos)
            {
                bos.close();
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }

}
}