来源自网络,很喜欢就收藏了;
需要注意的是:如果想要对这个实例进行测试的话,首先需要在c盘建立一个c://myjava的目录。然后将相应的java文件放在这个目录中。并将产生的.clas文件放在c://myjava/com/lg.test目录下,否则是找不到的。这是要注意的。。
class FileClassLoader :
|
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
|
package com.lg.test;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;/** * Created by 鏉庢灉 on 2016/8/6. */public class FileClassLoader extends ClassLoader { String rootDir=null; public FileClassLoader(String rootDir) { this.rootDir = rootDir; } @Override protected Class<?> findClass(String className) throws ClassNotFoundException { //首先检查是否已经被加载了。 Class<?> c = findLoadedClass(className); String path = rootDir + "/" + className.replace('.', '/') + ".class"; if (c != null) { return c; } else { /*双亲委托模式*/ ClassLoader loaderParent = this.getParent(); c = loaderParent.loadClass(className); if (c != null) { return c; } else { /*如果再不行的话,就再进行加载。因为字节码的本质就是一个字节数组*/ InputStream is = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { is = new FileInputStream(path); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } c = defineClass(className, buffer, 0, buffer.length); } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } return c; } }} |
class Demo :
|
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
|
package com.lg.test;/** * Created by 鏉庢灉 on 2016/8/6. *//*相同的类加载器对同一个类进行加载,得到的hascode是相同的 * 不同的类加载器对同一类进行加载,得到的hascode是不一样的*/public class Demo { public static void main(String[] args) { try { Class<?> c = loader.findClass("com.lg.test.HelloWorld"); Class<?> c0=loader.findClass("com.lg.test.HelloWorld"); Class<?> c1=loader2.findClass("com.lg.test.HelloWorld"); Class<?> c2=loader.findClass("com.lg.test.Demo01"); Class<?> c3=loader.findClass("java.lang.String"); System.out.println(c.hashCode()); System.out.println(c.getClassLoader()); System.out.println(c0.hashCode()); System.out.println(c0.getClassLoader()); System.out.println(c1.hashCode()); System.out.println(c1.getClassLoader()); System.out.println(c2.hashCode()); System.out.println(c2.getClassLoader()); System.out.println(c3.hashCode()); System.out.println(c3.getClassLoader()); } catch (ClassNotFoundException e) { e.printStackTrace(); } }} |
最后运行的结果为:
366712642
sun.misc.Launcher$AppClassLoader@4e0e2f2a
366712642
sun.misc.Launcher$AppClassLoader@4e0e2f2a
366712642
sun.misc.Launcher$AppClassLoader@4e0e2f2a
1829164700
sun.misc.Launcher$AppClassLoader@4e0e2f2a
2018699554
null