Java Class Loader

Source: Internet
Author: User

 

Introduction: Class Loader is an important concept in Java. The class loader is responsible for loading the byte code of the Java class to the Java Virtual Machine. This article first introduces in detail the basic concepts of Java class loaders, several types of loaders that come with Java virtual machines, and some misunderstandings in classpath settings in Java development.

Classloaders are an innovative Java language and an important reason for the popularity of Java. It enables the Java class to be dynamically loaded to the Java Virtual Machine and executed. The class loader has appeared since jdk1.0. It was initially developed to meet the needs of Java applet. The Java Applet needs to download and execute a Java class file from a remote browser. Currently, class loaders are widely used in Web containers and osgi. Generally, Java application developers do not need to directly interact with similar loaders. The default behaviors of Java virtual machines are enough to meet the needs of most cases. However, if you encounter a situation where you need to interact with the class loader, but you are not very familiar with the opportunity of the Class Loader, it is easy to spend a lot of time debugging exceptions such as classnotfoundexception and noclassdeffounderror. Class Loader is used to load Java classes to virtual machines. Generally, Java virtual machines use Java classes as follows: Java source files (. Java) are converted to Java Byte Code (. class files) after being compiled by the Java compiler ). The Class Loader reads JAVA byte code and converts it to an instance of the Java. Lang. Class class. Each such instance is used to represent a Java class. You can create an object of this class through the newinstance () method of the instance. The actual situation may be more complex. For example, java byte code may be dynamically generated by tools or downloaded over the network. The link and verification process may also be passed.
Java class loader mechanism: the parent delegation mechanism is used for Java class loading. This mechanism can better ensure the security of the Java platform. In this delegation mechanism, except the root loader that comes with the Java virtual machine, the other class loaders only have one parent loader. When the Java program requests loader1 to load the sample class, loader1 first delegates its parent loader to load the sample class. If the parent loader can load, the parent loader completes the loading task, otherwise, the loader1 loader loads the sample class. Which class loaders are included in the Java Virtual Machine? ● Root (bootstrap) Class Loader, also known as Bootstrap class loader. The loader does not have a parent loader. It is responsible for loading the Core Types of virtual machines, such as Java. Lang. In the subsequent test code, we can see that Java. Lang. object is loaded by the root class loader. More accurately, bootstrap loads the class library from the directory specified by the system property sun. boot. Class. Path. The implementation of the root loader depends on the underlying operating system. The implementation of attribute virtual machines is not written in Java, but may be C/C ++, therefore, it does not inherit Java. lang. classloader class. ● Exstension Class Loader: its parent loader is the root class loader. It loads the class library from the directory specified by the java. Ext. dirs system attribute. If you place the JAR File Created by the user in this directory, it will be automatically loaded by the extended tool. The extension class loader is a pure Java class. Is a subclass of the Java. Lang. classloader class. The classpath setting misunderstanding will be mentioned later. ● System Class Loader: it is also called an application Class Loader. Its Parent loader is an extension class loader. It loads classes from the environment variable classpath or from the directory specified by the system attribute java. Class. path. It is the default parent loader of the custom class loader. For example, the person class mentioned later. The system class loader is a pure Java class and a subclass of Java. Lang. classloader. Test and verification test test1: test the package COM. jfans; public class classloadertest1 {public static void main (string [] ARGs) {system. out. println (string. class. getclassloader (); // root (pilot) Class Loader system. out. println (object. class. getclassloader (); // root (pilot) Class Loader system. out. println (person. class. getclassloader (); // system class loader, custom person class system. out. println (sun.net. SPI. nameservice. DNS. dnsnameservice. class. getclassloader (); classloader loader = classloader. getsystemclassloader (); // returns the system class loader (generated when the VM is started) system. out. println (loader) ;}} result: nullnullsun. misc. launcher $ AppClassLoader@82ba41sun.misc.Launcher $ ExtClassLoader@923e30sun.misc.Launcher $ appclassloader @ 82ba41 result Description: The output is null, which indicates the root loader Test2: returns several paths related to the Class Loader package COM. jfans; public class classloadertest3 {public static void main (string [] ARGs) {string sunbootclasspath = system. getproperty ("Sun. boot. class. path "); system. out. println ("sunbootclasspath:" + sunbootclasspath); string javaextdirs = system. getproperty ("Java. ext. dirs "); system. out. println ("extdir:" + javaextdirs); string javaclasspath = system. getproperty ("Java. class. path "); system. out. println ("javaclasspath:" + javaclasspath); system. out. println (Java. lang. classloader. class. getclassloader () ;}} result: sunbootclasspath: e:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/RT. jar; E:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/i18n. jar; E:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/sunrsasign. jar; E:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/JSSE. jar; E:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/JCE. jar; E:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/charsets. jar; E:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/classesextdir: e:/developmenttools/jdk5.0/Java/jdk1.5.0 _ 05/JRE/lib/extjavaclasspath: D: /workspace_job/classloader/bin Description: The test platform is Windows XP, jdk5.0, And the JDK installation directory is E:/developmenttools/jdk5.0/Java/

Note: RT. jar is loaded by the root class loader. RT is short for runtime, which is the core Java class. Misunderstanding of classpath settings: I often see some people and some java textbooks write the classpath content :.; % java_home %/JRE/lib/RT. jar; other jar files may be added later. The above theoretical and test results show that RT. jar is actually loaded by the root class loader. Therefore, it is not necessary to set % java_home %/JRE/lib/RT. jar in classpaht; you only need to set it to. (current directory.

Related Article

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.