Classes loaders and dynamic loading of classes in Java

The basis for working with classes in Java is loader classes, ordinary Java objects that provide an interface for finding and creating a class object by its name while the application is running.

At the beginning of the program, 3 main class loaders are created:

  • basic bootloader (bootstrap/primordial). Loads the core JDK system and inner classes Core API - java.* packages (rt.jar and i18n.jar). It is important to note that the base loader is "Initial" or "Root" and is part of the JVM, so it cannot be created in-code programs.
  • extension loader (extention). Loads various extension packages that are located in the <JAVA_HOME>/lib/ext directory or another directory described in the java.ext.dirs system parameter. This allows you to update and add new extensions without having to modify the settings of the applications you are using. The extension loader is implemented by the sun.misc.Launcher$ExtClassLoader class.
  • system loader (system/application). Loads classes whose paths are specified in the CLASSPATH environment variable or paths that are specified on the JVM startup command line after the -classpath or -cp switches. The system loader is implemented by the sun.misc.Launcher$AppClassLoader class.

Class loaders are hierarchical: each of them (except for the base one) has a parent loader and in most cases, before trying to load a class on its own, it first sends a request to the parent loader to load the specified class. This delegation allows classes to be loaded with the loader closest to the base loader in the delegation hierarchy. As a result, the search for classes will occur in the sources in the order of their trust: first in the Core API library, then in the extensions folder, then in the local CLASSPATH files.

There are three parts to loading a class:

  • Loading - this phase is used to find and physically load the class file in a specific source (depending on the loader). This process defines the base representation of the class in memory. At this stage, concepts such as "methods", "fields", etc. not yet known.
  • Linking is a process that can be broken down into 3 parts:
    • Bytecode verification - checking the bytecode against the requirements defined in the JVM specification.
    • Class preparation is the creation and initialization of the necessary structures used to represent the fields, methods, implemented interfaces, etc., defined in the loaded class.
    • Resolving - loading a set of classes that the loaded class refers to.
  • Initialization - calling static initialization blocks and assigning default values to class fields.

Dynamic loading of classes in Java has a number of features:

  • lazy loading and binding of classes. Classes are loaded only when necessary, which saves resources and distributes the load.
  • checking the correctness of the loaded code (type safeness). All actions related to control of the use of types are performed only during class loading, thus avoiding additional load during code execution.
  • programmed loading. The custom loader has complete control over the process of obtaining the requested class - whether to search for the bytecode and create the class, or delegate creation to another loader. Additionally, it is possible to set various security attributes for loaded classes, thus allowing you to work with code from untrusted sources.
  • multiple namespaces. Each loader has its own namespace for the classes it creates. Accordingly, classes loaded by two different loaders based on common bytecode will be different on the system.

There are several ways to initiate loading of the required class:

  • explicit: calling ClassLoader.loadClass() or Class.forName() (by default, the loader that created the current class is used, but it is also possible to explicitly specify the loader);
  • implicit: when a previously unused class is required for the further operation of the application, the JVM initiates its loading.

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed