What are the three class loaders?
Java 中的三个主要类加载器是 Bootstrap ClassLoader、Extension ClassLoader 和 Application ClassLoader,它们构成了双亲委派模型。1. Bootstrap ClassLoader 是最顶层的类加载器,由 C/C++ 实现,负责加载 Java 核心类库(如 rt.jar),位于 jre/lib 目录下,用户无法直接访问;2. Extension ClassLoader 是其子类加载器,负责加载 jre/lib/ext 路径下的扩展类库,可通过 ClassLoader.getSystemClassLoader().getParent() 获取;3. Application ClassLoader 是系统类加载器,负责加载 classpath 下的应用程序类,是最常用的类加载器,通过 ClassLoader.getSystemClassLoader() 获取。类加载时优先由父类加载器尝试加载,失败后再由子类加载器处理,确保核心类不被重复加载或篡改。
Java 中有三个主要的类加载器(Class Loaders),它们负责在运行时动态加载类到 JVM 中。这三个类加载器分别是:Bootstrap ClassLoader、Extension ClassLoader 和 Application ClassLoader。它们构成了 Java 类加载的层次结构,也称为“双亲委派模型”。
Bootstrap ClassLoader
这是最顶层的类加载器,由 C/C++ 实现,不继承 ClassLoader
类。它负责加载 Java 的核心类库,比如 rt.jar
中的内容,包括 java.lang.*
、java.util.*
这些基础类。
- 通常位于
jre/lib
目录下 - 用户无法直接访问这个类加载器
- 是 Extension 和 Application ClassLoader 的“父加载器”
Extension ClassLoader
这个类加载器是 sun.misc.Launcher$ExtClassLoader
的实例,它的父类加载器是 Bootstrap ClassLoader。
- 负责加载 Java 的扩展类库,默认加载路径是
jre/lib/ext
或者系统环境变量java.ext.dirs
指定的路径 - 开发者可以将自己的 JAR 包放到该目录下,JVM 会自动加载这些类
- 可以通过代码获取其实例
例如:
ClassLoader extClassLoader = ClassLoader.getSystemClassLoader().getParent();
Application ClassLoader
也叫系统类加载器(System ClassLoader),它是 sun.misc.Launcher$AppClassLoader
的实例,负责加载应用程序类路径(classpath)下的类。
- 是用户最常打交道的类加载器
- 加载我们自己写的类或者第三方库(如 JAR 文件)
- 默认的类加载器,如果我们没有自定义类加载器,JVM 就使用它来加载类
可以通过以下方式获取:
ClassLoader appClassLoader = ClassLoader.getSystemClassLoader();
简单总结一下这三者的加载顺序和关系:
- 当一个类需要被加载时,首先由当前类加载器委托给父类加载器去尝试加载
- 只有当父类加载器无法加载时,才会由子类加载器尝试加载
- 这种机制保证了 Java 核心类不会被重复加载或篡改
基本上就这些。
The above is the detailed content of What are the three class loaders?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

SetupaMaven/GradleprojectwithJAX-RSdependencieslikeJersey;2.CreateaRESTresourceusingannotationssuchas@Pathand@GET;3.ConfiguretheapplicationviaApplicationsubclassorweb.xml;4.AddJacksonforJSONbindingbyincludingjersey-media-json-jackson;5.DeploytoaJakar

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

Use performance analysis tools to locate bottlenecks, use VisualVM or JProfiler in the development and testing stage, and give priority to Async-Profiler in the production environment; 2. Reduce object creation, reuse objects, use StringBuilder to replace string splicing, and select appropriate GC strategies; 3. Optimize collection usage, select and preset initial capacity according to the scene; 4. Optimize concurrency, use concurrent collections, reduce lock granularity, and set thread pool reasonably; 5. Tune JVM parameters, set reasonable heap size and low-latency garbage collector and enable GC logs; 6. Avoid reflection at the code level, replace wrapper classes with basic types, delay initialization, and use final and static; 7. Continuous performance testing and monitoring, combined with JMH

Maven is a standard tool for Java project management and construction. The answer lies in the fact that it uses pom.xml to standardize project structure, dependency management, construction lifecycle automation and plug-in extensions; 1. Use pom.xml to define groupId, artifactId, version and dependencies; 2. Master core commands such as mvnclean, compile, test, package, install and deploy; 3. Use dependencyManagement and exclusions to manage dependency versions and conflicts; 4. Organize large applications through multi-module project structure and are managed uniformly by the parent POM; 5.

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa
