Java Basics _ ClassLoader

Source: Internet
Author: User

What is a class loader

    • The ClassLoader was introduced in the Java language version 1.0. It was originally designed to meet javaapplet needs. The class loader is now widely used in web containers and OSGi, where developers of Java applications do not need to interact directly with the same loader. The default behavior of the Java Virtual machine is sufficient to meet the needs of most situations. However, if you encounter the need to interact with the ClassLoader, and the mechanism of the ClassLoader is not very well understood, it is easy to spend a lot of time on classnotfoundexception and Noclassdeffounderror and other anomalies.
    • As the name implies, the class loader is used to load Java classes into a Java virtual machine. In general, Java virtual machines use Java classes in the following way: Java source programs (. java files) are converted to Java bytecode codes (. class files) after they are compiled by the Java compiler. The ClassLoader is responsible for reading the Java byte code and converting it into an instance of the Java.lang.Class class. Each such instance is used to represent a Java class. An object of the class can be created by using the Newinstance () method of this instance. The actual situation may be more complex, such as Java bytecode may be generated dynamically through the tool, or it may be downloaded over the network. Basically all ClassLoader are an instance of the Java.lang.ClassLoader class.

ClassLoader Class Introduction

The basic function of the Java.lang.ClassLoader class is to find or generate its corresponding byte code based on the name of a specified class, and then define a Java class from these byte codes, i.e. an instance of the Java.lang.Class class, in addition to which ClassLoader is responsible for loading Java applications require resources, like files and configuration files. To complete the load class, ClassLoader provides a series of methods.

Methods related to load classes in table 1:classloader

Method Description
GetParent () Returns the parent class loader for this class
LoadClass (String name) Load the class named name, and the returned result is an instance of the Java.lang.Class class
Findclass (String name) Look for a class named name, and return the result of an instance of the Java.lang.Class class
Findloadedclass (String name) Find the already loaded class named Name, and the returned result is an instance of Java.lang.Class
DefineClass (String name,byte[] b,int off,int len) Converts the contents of byte array B into a Java class, and the returned result is an instance of Java.lang.Class. This method is declared final.
Resolveclass (class<?> c) Link-Developed Java classes

In the method given in table 1, the value of the name parameter that represents the class name is the binary name of the class. It is important to note the representation of the inner class, such as Com.exampe.husband$wife and Com.example.outclass$innerclass.

The tree-like structure of the ClassLoader

Class Loaders in Java can be broadly divided into two categories, one for the system, and one for Java application developers to write themselves. The system provides the following three kinds of main:

    • Boot class loader (bootstrap class loader): It is used to load the Java core Library, which is implemented with native code and is not integrated from Java.lang.ClassLoader.
    • Extension class loader (Extensions class loader): It is used to load the Java extension Library. The implementation of the Java Virtual Machine provides an extension library directory. The ClassLoader finds and loads the Java class in this directory.
    • System class Loader (loader): He loads Java classes based on the Classpath (CLASSPATH) of the Java application. In general, Java-applied classes are loaded by it. It can be obtained by Classloader.getsystemclassloader ();

In addition to the system-provided ClassLoader, developers can java.lang.ClassLoader implement their own ClassLoader by inheriting classes to meet some special requirements.

Figure 1. Class Loader tree structure

Too tired ... Write tomorrow

Java Basics _ ClassLoader

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.