- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- public class MyClassLoader2 extends ClassLoader
- {
- private String name ;
- private String path= "d:\\";
- public MyClassLoader2(String name)
- {
- super();
- this.name = name;
- }
- public MyClassLoader2(ClassLoader parent , String name)
- {
- super(parent);
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
- public String getPath()
- {
- return path;
- }
- public void setPath(String path)
- {
- this.path = path;
- }
- public String toString()
- {
- return this.name;
- }
- @Override
- protected Class<?> findClass(String name) throws ClassNotFoundException
- {
- byte[] date = this.loadClassDate(name);
- return this.defineClass(name , date , 0 ,date.length);
- }
- public byte[] loadClassDate(String name)
- {
- InputStream is = null;
- ByteArrayOutputStream baos = null;
- byte[] date = null;
- try
- {
- baos = new ByteArrayOutputStream();
- is = new FileInputStream(new File(this.path + name+ ".class"));
- int b = 0;
- while( (b = is.read()) != -1)
- {
- baos.write(b);
- }
- date = baos.toByteArray();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- is.close();
- baos.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- return date;
- }
- }
复制代码 以上代码我只知道写,但是不知道具体的执行流程和细节?loadClass()这个方法到底是执行那些代码? |