Instance parsing of the custom class loader in Java, java instance
This article focuses on the parsing of custom classloaders in Java, as detailed below.
Self-written Class Loader
Note: If you want to test this instance, you must first create a c: // myjava directory on drive c. Then place the corresponding java file in this directory. And put the generated. clas file in the c: // myjava/com/lg. test directory. Otherwise, it cannot be found. This is worth noting ..
Class FileClassLoader:
Package com. lg. test; import java. io. byteArrayOutputStream; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream;/*** Created by your authorized users 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 delegation mode */ClassLoader loaderParent = this. getParent (); c = loaderParent. loadClass (className); if (c! = Null) {return c;} else {/* If not, load it 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);} catch (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 province has been Created on. * // * the same class loader loads the same class. The hascode is the same. * different class loaders load the same class, the resulting 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 ("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 running 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 want to define a Network Class Loader, you need to use a URL. Note This.
You can change the value of rootDie to com.bjsxt.cn. Then you can use Url. openStream.
Summary
The above is all the content about the parsing of the custom Class Loader instance in Java, and I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!