JAVA class loader.

Source: Internet
Author: User

The JVM Specification defines two types of class loaders: Start the internal loader (bootstrap) and user-defined loader (user-defined class loader ). 1. Basic concepts of ClassLoader 1. ClassLoader class loaders are used to load classes into JVM. The JVM Specification defines two types of class loaders: Start the internal loader (bootstrap) and user-defined loader (user-defined class loader ). At runtime, the JVM generates three ClassLoader: Bootstrap ClassLoader, Extension ClassLoader, and AppClassLoader. bootstrap is written in C ++. We cannot see it in Java. It is null and is the class loader that comes with JVM. It is used to load core class libraries, such as java. lang.. The Parent of AppClassLoader is ExtClassLoader, while the Parent of ExtClassLoader is Bootstrap ClassLoader. Java provides the abstract class ClassLoader. All user-defined class loaders instantiate the class of the class loader. System Class Loader is a special user-defined Class Loader provided by the JVM implementer. It is loaded by default when the programmer does not specify a Loader. The system class loader can be obtained through the ClassLoader. getSystemClassLoader () method. Example 1: Test the JVM ClassLoader/* LoaderSample1.java */public class LoaderSample1 {public static void main (String [] args) {Class c; ClassLoader cl; cl = ClassLoader. getSystemClassLoader (); System. out. println (cl); while (cl! = Null) {cl = cl. getParent (); System. out. println (cl);} try {c = Class. forName ("java. lang. object "); cl = c. getClassLoader (); System. out. println ("java. lang. object's loader is "+ cl); c = Class. forName ("LoaderSample1"); cl = c. getClassLoader (); System. out. println ("LoaderSample1's loader is" + cl);} catch (Exception e) {e. printStackTrace () ;}} running result Sun on my machine (sun Java 1.4.2. misc. launcher $ AppCla SsLoader@1a0c10fsun.misc.Launcher $ ExtClassLoader @ e2eec8null java. lang. object's loader is nullLoaderSample1's loader is sun. misc. launcher $ AppClassLoader @ 1a0c10f indicates that the system class loader is instantiated from the sun class. misc. the second line of Launcher $ AppClassLoader indicates that the parent of the system class loader is instantiated from the sun class. misc. the third line of Launcher $ ExtClassLoader indicates that the parent of the system class loader is the fourth line of bootstrap, and the core class is java. lang. the Object is represented by the fifth line of bootstrap load. The user class LoaderSample1 is the second part of the system class loader. namespace and its functions each class loader has its own namespace The namespace is composed of all classes that use this loader as the founding class loader. The two classes in different namespaces are invisible, but you can still access the classes in another namespace as long as you get the reference of the Class Object corresponding to the Class. Example 2 demonstrates how a namespace class uses another namespace class. In this example, LoaderSample2 is loaded by the system class loader and LoaderSample3 is loaded by the custom loader. The two classes are not in the same namespace, however, LoaderSample2 obtains the reference of the Class Object corresponding to LoaderSample3, so it can access public members (such as age) in loadersampl3 ). Example 2 Access to classes in different namespaces/* LoaderSample2.java */import java.net. *; import java. lang. reflect. *; public class LoaderSample2 {public static void main (String [] args) {try {String path = System. getProperty ("user. dir "); URL [] us = {new URL (" file: // "+ path +"/sub/")}; ClassLoader loader = new URLClassLoader (us ); class c = loader. loadClass ("LoaderSample3"); Object o = c. newInstance (); Field f = c. getField ("Age"); int age = f. getInt (o); System. out. println ("age is" + age);} catch (Exception e) {e. printStackTrace () ;}}/ * sub/Loadersample3.java */public class LoaderSample3 {static {System. out. println ("LoaderSample3 loaded");} public int age = 30;} compile: javac LoaderSample2.java; javac sub/LoaderSample3.java run: java LoaderSample2LoaderSample3 loadedage is 30 as shown in the running result, you can create a class in another namespace in class LoaderSample2. Objects in LoaderSample3 can access their public member age. The runtime package consists of classes of the same package that are loaded by the same class loader and determines whether the two classes belong to the same runtime package, it depends not only on whether their package names are the same, but also on whether the class loaders are the same. Only classes belonging to the same runtime package can access classes and members visible to each other. This restriction avoids the situation where the user's code impersonates the class of the core class library and accesses the visible member of the core class library package. Assume that you have defined a java class. lang. yes, it is loaded with a user-defined class loader, Because java. lang. yes and core class library java. lang. * loaded by different loaders, which belong to different runtime packages, so java. lang. yes, you cannot access the core class library java. visible member of the class package in lang. In summary, the namespace does not completely prohibit access to classes in different spaces. The parent-parent delegated model enhances Java security, and the runtime package adds protection for visible package members. Ii. Extend the ClassLoader method. We aim to load a class from the local file system using our ClassLoader. To create our own class loaders, we should extend the ClassLoader class, which is an abstract class. We create a FileClassLoader extends ClassLoader. We need to override the findClass (String name) method in ClassLoader. This method obtains a Class object through the Class name. Public Class findClass (String name) {byte [] data = loadClassData (name); return defineClass (name, data, 0, data. length);} We should also provide a method loadClassData (String name) to return the class file's section array through the class name. Then we can use the defineClass () method provided by ClassLoader to return the Class object. Public byte [] loadClassData (String name) {FileInputStream FCM = null; byte [] data = null; try {FD = new FileInputStream (new File (drive + name + fileType )); byteArrayOutputStream baos = new ByteArrayOutputStream (); int ch = 0; while (ch = FCM. read ())! =-1) {baos. write (ch);} data = baos. toByteArray ();} catch (IOException e) {e. printStackTrace ();} return data ;}

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.