1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
public class MyClassLoader extends ClassLoader { private String name;
private String path = "/home/terwer/Downloads";
private final String fileType = ".class";
public String getPath() { return path; }
public void setPath(String path) { this.path = path; }
public MyClassLoader(String name) { super(); this.name = name; }
public MyClassLoader(ClassLoader parent, String name) { super(parent); this.name = name; }
@Override public String toString() { return this.name; }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { byte[] data = this.loadClassData(name);
return this.defineClass(name, data, 0, data.length); }
private byte[] loadClassData(String name) { InputStream is = null; byte[] data = null; ByteArrayOutputStream baos = null;
try { this.name = this.name.replace(".", "/"); is = new FileInputStream(path + "/" + name + fileType);
baos = new ByteArrayOutputStream();
int ch = 0; while (-1 != (ch = is.read())) { baos.write(ch); }
data = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (baos != null) { baos.close(); } } catch (Exception e2) { e2.printStackTrace(); } }
return data; } }
|