Class Loader and custom Class Loader (67), custom 67

Source: Internet
Author: User

Class Loader and custom Class Loader (67), custom 67

Class Loading

All class loaders are subclasses of ClassLoader.

The class Loader always follows the. class running directory.

You can use the following methods to read files under the root directory of classpath:

1. In Java projects, you can obtain files under classspath in the following ways:

Public void abc () {// read each method. Use a class to obtain Appclassloader ClassLoader cl = ReadFile. class. getClassLoader (); URL url = cl. getResource ("a.txt"); System. err. println ("url1 is:" + url. getPath (); // Method 2: directly use the ClassLoader URL url2 = ClassLoader. getSystemResource ("a.txt"); System. err. println ("url2 is:" + url2.getPath ());}

In Tomcat, tomcat declares two class loaders:

StandardClassLoader-load tomcat/lib/*. jar-serlvetapi. jar

Webappclassloader/load tomcat/webapps/project/web-inf/lib/*. jar & web-inf/classes/*. class

 

In any project, the loader for retrieving classes should use the following methods:

SomeClass (you wrote). class. getClassLoader (). getResource; get the class loader of this class

In the java project: AppClassLoader

In a Web Project: WebAppClassLoader

Public class OneServlet extends HttpServlet {@ Override public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ClassLoader loader = OneServlet. class. getClassLoader (); // WebAppClassLoader int index = 1; while (loader! = Null) {System. err. println (index ++) + "Class loader:" + loader. getClass (); loader = loader. getParent (); // get parent class loader }}}

Running result:

Class 1 loader is: class org. apache. catalina. loader. the WebappClassLoader2 class loader is: class org. apache. catalina. loader. standardClassLoader3 class loader is: class sun. misc. launcher $ AppClassLoader4 class loaders are: class sun. misc. launcher $ ExtClassLoader
Custom Class Loader

Which class loader does JDK use to read the bytecode of Class A, then Class A is loaded by the class loader.

Whether a class with the same name can be converted to each other depends on whether it is in the same class loader.

Package cn. hx. demo; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; import java.net. URL; public class MyClassLoader2 extends ClassLoader {/*** name: cn. itcast. demo. person * is found based on the package name. class file * cn. itcast. demo. person => cn/itcast/demo/Person. class */public Class <?> FindClass (String name) throws ClassNotFoundException {String classNameWithPackage = name; Class <?> Cls = null; try {// set name = name first. replace (". ","/"); name + = ". class "; // determine the directory URL = MyClassLoader2.class. getClassLoader (). getResource (name); System. err. println (">>:" + url. getPath (); File file = new File (url. getPath (); InputStream in = new FileInputStream (file); // read this. byte [] B = new byte [in. available ()]; // directly declare that the size of this byte is the size of this file int len = in. read (B); // len = 621 System. err. println (len);/*** the first parameter is the class name */cls = defineClass (classNameWithPackage, B, 0, len);} catch (Exception e) {e. printStackTrace () ;}return cls ;}}

Test class custom Class Loader

Public class ClassLoaderDemo {public static void main (String [] args) throws Exception {MyClassLoader2 mc = new MyClassLoader2 (); Class cls = mc. findClass ("cn. itcast. demo. person "); Object o = cls. newInstance (); System. err. println ("toString:" + o + "," + o. getClass (). getClassLoader (); // directly use peron as the AppClassLoader System. err. println (">>:" + Person. class. getClassLoader (); // Since o is loaded by mc. The Person is loaded by the App, and all cannot be converted to = from two different loaders // Person p = (Person) o; // type conversion error ClassCastException // System. err. println (p );}}

 

 

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.