Instance parsing of the custom class loader in Java, java instance

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.