Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the Java class loader

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to analyze the Java class loader, the content is very detailed, interested friends can refer to, hope to be helpful to you.

As the name implies, the class loader (class loader) is used to load Java classes into the Java virtual machine. In general, the Java virtual machine uses the Java class in the following way: the Java source program (.java file) is converted into Java bytecode (.class file) after being compiled by the Java compiler. The classloader is responsible for reading the Java bytecode and converting it to an instance of the java.lang.Class class. Each such instance is used to represent a Java class. An object of this class can be created through the newInstance () method of this instance. The actual situation may be more complex, such as the Java bytecode may be generated dynamically through the tool, or it may be downloaded over the network.

Basically all classloaders are an instance of the java.lang.ClassLoader class. This Java class is described in more detail below.

Introduction to the java.lang.ClassLoader class

The basic responsibility of the java.lang.ClassLoader class is to find or generate the corresponding bytecode based on the name of a specified class, and then define a Java class, an instance of the java.lang.Class class, from these bytecodes.

ClassLoader provides a series of methods, such as the more important ones:

Method description

GetParent () returns the parent class loader of this class loader.

LoadClass (String name) loads a class named name and returns an instance of the java.lang.Class class.

FindClass (String name) looks for a class named name and returns an instance of the java.lang.Class class.

FindLoadedClass (String name) looks for a class that has been loaded with the name name and returns an instance of the java.lang.Class class.

DefineClass (String name, byte [] b, int off, int len) converts the contents of the byte array b into a Java class, and the result is an instance of the java.lang.Class class. This method is declared as final.

ResolveClass (Class c) links to the specified Java class.

Tree structure of class loader

Class loaders in Java can be roughly divided into two categories, one provided by the system and the other written by Java application developers. There are three main class loaders provided by the system:

Bootstrap class loader (bootstrap class loader): the core library used to load Java is implemented in native code and does not inherit from java.lang.ClassLoader.

Extension class loader (extensions class loader): it is used to load the extension library of Java. The implementation of the Java virtual machine provides an extended library directory. The class loader looks for and loads the Java class in this directory.

System class loader (system class loader): it loads the Java class based on the classpath (CLASSPATH) of the Java application. Generally speaking, it loads all the classes of Java applications. You can get it through ClassLoader.getSystemClassLoader ().

In addition to the classloader provided by the system, developers can implement their own classloader by inheriting the java.lang.ClassLoader class to meet some special needs.

Except for the bootstrap classloader, all classloaders have a parent class loader. This can be obtained through the getParent () method given in the table above. For the classloader provided by the system, the parent class loader of the system class loader is the extended class loader, and the parent class loader of the extended class loader is the boot class loader; for the class loader written by the developer, the parent class loader is the class loader that loads the Java class of this kind of loader. Because the class loader Java class, like other Java classes, is loaded by the class loader. In general, the parent class loader of a class loader written by a developer is the system class loader. Class loaders are organized in this way to form a tree structure. The root node of the tree is the boot class loader. The following figure shows a typical tree organization diagram of the classloader, where the arrow points to the parent class loader.

On how to parse the Java class loader to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report