Skip to content

Commit 5fa4592

Browse files
author
jossc
committed
jvm-classloader
1 parent 4c5aac5 commit 5fa4592

9 files changed

Lines changed: 179 additions & 10 deletions

File tree

src/main/java/com/jvm/classloader/classloader/ClassLoaderTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void setPath(String path) {
2222
this.path = path;
2323
}
2424

25-
ClassLoaderTest(String className) {
25+
public ClassLoaderTest(String className) {
2626
super();
2727
this.className = className;
2828
}
@@ -34,17 +34,16 @@ public ClassLoaderTest(ClassLoader parent, String className) {
3434

3535
public static void main(String[] args) throws Exception {
3636
ClassLoaderTest classLoaderTest = new ClassLoaderTest("loader");
37-
//classLoaderTest.setPath("/Users/demons/IdeaProjects/JavaCode/target/classes/com/jvm/classloader/relyclass/");
38-
classLoaderTest.setPath("/Users/demons/Desktop/");
39-
Class aClass = classLoaderTest.loadClass("com.jvm.classloader.relyclass.TestOne");
37+
classLoaderTest.setPath("/Users/demons/IdeaProjects/JavaCode/target/classes/com/jvm/classloader/relyclass/");
38+
/*classLoaderTest.setPath("/Users/demons/Desktop/");*/
39+
Class aClass = classLoaderTest.loadClass("com.jvm.classloader.relyclass.Test1");
4040
Object object = aClass.newInstance();
4141
System.out.println(object.getClass().getName());
4242

4343
}
4444

4545
public static void test(ClassLoader classLoader) throws Exception {
4646
Class<?> clazz = classLoader.loadClass("com.jvm.classloader.relyclass.TestOne");
47-
4847
Object object = clazz.newInstance();
4948
System.out.println(object.getClass().getName());
5049

@@ -54,7 +53,7 @@ public static void test(ClassLoader classLoader) throws Exception {
5453
protected Class<?> findClass(String name) {
5554
System.out.println("find Class :" + name);
5655
byte[] data = loadClassData(name);
57-
return defineClass(className, data, 0, data.length);
56+
return defineClass(name, data, 0, data.length);
5857
}
5958

6059
@Override
@@ -64,14 +63,15 @@ public String toString() {
6463
'}';
6564
}
6665

67-
private byte[] loadClassData(String name) {
68-
System.out.println("name:" + name);
66+
private byte[] loadClassData(String className) {
67+
System.out.println("className:" + className);
6968
InputStream is = null;
7069
byte[] data = null;
7170
ByteArrayOutputStream byteArrayOutputStream = null;
7271
try {
73-
name = name.replace(".", "/");
74-
is = new FileInputStream(new File(path + name + fileExtension));
72+
className = className.replace(".", "/");
73+
System.out.println(className);
74+
is = new FileInputStream(new File(path + className + fileExtension));
7575
byteArrayOutputStream = new ByteArrayOutputStream();
7676
int ch;
7777
while (-1 != (ch = is.read())) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jvm.classloader.relyclass;
2+
3+
/**
4+
* @ClassName Test1
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-25 22:06
8+
**/
9+
public class Test1 {
10+
static {
11+
System.out.println(Test1.class.getClassLoader());
12+
}
13+
14+
public static void main(String[] args) {
15+
System.out.println(System.getProperty("sun.boot.class.path"));
16+
System.out.println(System.getProperty("java.ext.dirs"));
17+
System.out.println(System.getProperty("java.class.path"));
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.jvm.classloader.relyclas;
2+
3+
import com.sun.crypto.provider.AESKeyGenerator;
4+
5+
/**
6+
* @ClassName Test2
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-06-26 22:35
10+
**/
11+
public class Test2 {
12+
public static void main(String[] args) {
13+
AESKeyGenerator aesKeyGenerator = new AESKeyGenerator();
14+
//sun.misc.Launcher$ExtClassLoader@13221655
15+
System.out.println(aesKeyGenerator.getClass().getClassLoader());
16+
System.out.println(Test2.class.getClassLoader());
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jvm.classloader.relyclass;
2+
3+
import com.jvm.classloader.classloader.ClassLoaderTest;
4+
5+
import java.lang.reflect.Method;
6+
7+
/**
8+
* @ClassName Test3
9+
* @Author chenzhuo
10+
* @Version 1.0
11+
* @Date 2019-06-26 23:03
12+
**/
13+
public class Test3 {
14+
public static void main(String[] args) throws Exception {
15+
ClassLoaderTest classLoader1 = new ClassLoaderTest("loader1");
16+
ClassLoaderTest c2 = new ClassLoaderTest("loader2");
17+
Class<?> clazz1 = classLoader1.loadClass("com.jvm.classloader.relyclass.TestObject");
18+
Class<?> clazz2 = c2.loadClass("com.jvm.classloader.relyclass.TestObject");
19+
System.out.println(clazz1==clazz2);
20+
Object o1 = clazz1.newInstance();
21+
Object o2 = clazz2.newInstance();
22+
23+
Method method = clazz1.getMethod("setTestObject",Object.class);
24+
method.invoke(o1,o2);
25+
26+
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.jvm.classloader.relyclass;
2+
3+
/**
4+
* @ClassName TestObject
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-26 22:56
8+
**/
9+
public class TestObject {
10+
private TestObject testObject;
11+
12+
public void setTestObject(Object testObject) {
13+
System.out.println(testObject);
14+
this.testObject = (TestObject) testObject;
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.share.classloader;
2+
3+
/**
4+
* @ClassName AppClassLoader1
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-29 18:15
8+
**/
9+
public class AppClassLoader1 {
10+
11+
public static void main(String[] args) {
12+
//这是appClassLoader加载的
13+
System.out.println(AppClassLoader1.class.getClassLoader());
14+
//获取appClassLoader的类加载路径
15+
System.out.println(System.getProperty("java.class.path"));
16+
//ExtClassLoader
17+
System.out.println(AppClassLoader1.class.getClassLoader().getParent());
18+
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.share.classloader;
2+
3+
import java.net.URL;
4+
5+
/**
6+
* @ClassName BootstrapClassloader1
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-06-29 18:24
10+
**/
11+
public class BootstrapClassloader1 {
12+
public static void main(String[] args) {
13+
URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
14+
for (URL url : urls) {
15+
System.out.println(url.toExternalForm());
16+
}
17+
18+
ClassLoader cl = String.class.getClassLoader();
19+
System.err.println(cl);
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.share.classloader;
2+
3+
import com.sun.crypto.provider.AESKeyGenerator;
4+
5+
/**
6+
* @ClassName ExtClassloader1
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-06-29 18:20
10+
**/
11+
public class ExtClassloader1 {
12+
13+
public static void main(String[] args) {
14+
System.out.println(System.getProperty("java.ext.dirs"));
15+
AESKeyGenerator aesKeyGenerator = new AESKeyGenerator();
16+
ClassLoader classLoader = aesKeyGenerator.getClass().getClassLoader();
17+
//sun.misc.Launcher$ExtClassLoader@13221655
18+
System.out.println(classLoader);
19+
//null
20+
System.out.println(classLoader.getParent());
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 内部分享的一些知识点
3+
*public Launcher()
4+
* {
5+
* ExtClassLoader localExtClassLoader;
6+
* try
7+
* { //(1)首先创建了ExtClassLoader
8+
* localExtClassLoader = ExtClassLoader.getExtClassLoader();
9+
* }
10+
* catch (IOException localIOException1)
11+
* {
12+
* throw new InternalError("Could not create extension class loader");
13+
* }
14+
* try
15+
* { //(2)然后以ExtClassloader作为父加载器创建了AppClassLoader
16+
* this.loader = AppClassLoader.getAppClassLoader(localExtClassLoader);
17+
* }
18+
* catch (IOException localIOException2)
19+
* {
20+
* throw new InternalError("Could not create application class loader");
21+
* } //(3)这个是个特殊的加载器后面会讲到,这里只需要知道默认下线程上下文加载器为appclassloader
22+
* Thread.currentThread().setContextClassLoader(this.loader);
23+
* }
24+
*/
25+
package com.share;

0 commit comments

Comments
 (0)