This article mainly studies related contents for the analysis of custom class loader instances in the Java language, as follows.
The class loader I wrote
It should be noted that if you want to test this instance, you must first create a c://myjava directory on the c drive. Then put the corresponding java file in this directory. And put the generated .clas file in the c://myjava/com/lg.test directory, otherwise it will not be found. This is something to be paid attention to. .
class FileClassLoader:
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 {//First check whether it has been loaded. Class<?> c = findLoadedClass(className);String path = rootDir + "/" + className.replace('.', '/') + ".class";if (c != null) {return c;} else {/*Parent delegate mode*/ClassLoader loaderParent = this.getParent();c = loaderParent.loadClass(className);if (c != null) {return c;} else {/*If it doesn't work, load again. Because the essence of bytecode is a byte array*/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);}c = defineClass(className, buffer, 0, buffer.length);}c (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();}catch (IOException e) {e.printStackTrace();}}}} return c;}}}}}class Demo:
package com.lg.test;/** * Created by on 2016/8/6. *//*The same class loader loads the same class, and the hascode is the same. Different class loaders load the same class, and the hascode is different. */public class Demo {public static void main(String[] args) {FileClassLoader loader = new FileClassLoader("c://myjava");FileClassLoader loader2=new FileClassLoader("c://myjava");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("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();}}}The final result is:
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
If you define a network class loader, you need to use a URL. This is something to be paid attention to.
You can change the value of rootDie to com.bjsxt.cn. Then use Url.openStream().
The above is all about the analysis of custom class loader instances in Java language. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!