0% found this document useful (0 votes)
18 views4 pages

Java Class Loader Overview and Types

The document discusses class loaders in Java, which are responsible for loading Java classes into the JVM. It describes the different types of class loaders including bootstrap, extension, and system class loaders and their hierarchical relationship. It also discusses how class loaders work internally and how to implement a custom network class loader.

Uploaded by

harendra tomar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Java Class Loader Overview and Types

The document discusses class loaders in Java, which are responsible for loading Java classes into the JVM. It describes the different types of class loaders including bootstrap, extension, and system class loaders and their hierarchical relationship. It also discusses how class loaders work internally and how to implement a custom network class loader.

Uploaded by

harendra tomar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction Of Class Loader In Java

Introduction:

In this article, you will learn about Class loader and its types in Java.

Java class loader is used to load a Java class files into Java virtual machine. It is a sub system of JVM.

In other words, a class loader is an object which is responsible for loading classes. The
class ClassLoader is an abstract class. A class loader should attempt to generate data which constitutes a
definition for the class. A usual strategy is to transform the name into a file name and then read a "class
file" of that name from a file system. Each Class object contains a reference to the ClassLoader which
defined it.

Class objects for array classes are not created by class loaders, but are created automatically as required
by the Java runtime. The class loader for an array class, as returned by [Link]() is the same
as the class loader for its element type; if the element type is a primitive type, then the array class has
no class loader.

Applications implement subclasses of ClassLoader in order to extend the manner in which the Java
virtual machine dynamically loads classes.

Class loaders may typically be used by security managers to indicate security domains.

The ClassLoader class uses a delegation model to search for classes and resources. Each instance
of ClassLoader has an associated parent class loader. When requested to find a class or resource,
a ClassLoader instance will delegate the search for the class or resource to its parent class loader before
attempting to find the class or resource itself. The virtual machine's built-in class loader, called the
"bootstrap class loader", does not itself have a parent but may serve as the parent of
a ClassLoader instance.

Class loaders that support concurrent loading of classes are known as parallel capable class loaders and
are required to register themselves at their class initialization time by invoking
the [Link]. Note that the ClassLoader class is registered as
parallel capable by default. However, its subclasses still need to register themselves if they are parallel
capable.
In environments in which the delegation model is not strictly hierarchical, class loaders need to be
parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the
duration of the class loading process (see loadClass methods).

Normally, the Java virtual machine loads classes from the local file system in a platform-dependent
manner. For example, on UNIX systems, the virtual machine loads classes from the directory defined by
the CLASSPATH environment variable.

However, some classes may not originate from a file; they may originate from other sources, such as the
network, or they could be constructed by an application. The method defineClass converts an array of
bytes into an instance of classClass. Instances of this newly defined class can be created
using [Link].
The methods and constructors of objects created by a class loader may reference other classes. To
determine the class(es) referred to, the Java virtual machine invokes the loadClass method of the class
loader that originally created the class.

For example, an application could create a network class loader to download class files from a server.
Sample code might look like:

ClassLoader loader = new NetworkClassLoader(host, port);


Object main = [Link]("Main", true).newInstance();
...

The network class loader subclass must define the methods findClass and loadClassData to load a class
from the network. Once it has downloaded the bytes that make up the class, it should use the
method defineClass to create a class instance. A sample implementation is:

class NetworkClassLoader extends ClassLoader {


String host;
int port;

public Class findClass(String name) {


byte[] b = loadClassData(name);
return defineClass(name, b, 0, [Link]);
}

private byte[] loadClassData(String name) {


// load the class data from the connection
...
}
}

Binary names

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as
defined by The Java™ Language Specification.

Examples of valid class names include:

"[Link]"
"[Link]$DefaultEditor"
"[Link]$Builder$FileBuilder$1"
"[Link]$3$1"

Various types of Java Class Loaders in Java are-


 Bootstrap Class loader.
 Extensions Class loader.
 System Class loader.

Bootstrap Class loader:

Bootstrap class loader is mainly used to load Java classes like [Link], [Link] etc. All the classes are
the part of JRE (Java Runtime Environment). It is a native implementation in Java.

Extensions Class loader:

Extensions class loader loads the classes from ext folder. When we use the system environment
property [Link], we can add these ‘ext’ folders and jar files to be loaded, using extensions class
loader.

System Class loader:

System classloader loads the classes, which are available in the Java classpath and these are loaded ,
using system class loader.

All class loaders have a hierarchical relationship between them. Class loader can load the classes from
one level above its hierarchical.

First level is a bootstrap class loader.

Second level is extensions class loader.

Third level is a system class loader.

 How Java class loader works internally

When we give a class name, class loader first checks the location of the class, reads a class file of
that name from the native file system. It is a subsystem of JVM. Thus, this loading process is
platform dependent.

In Java, by default, [Link] is registered as a classloader. This is sufficient to load the


classes in parallel. The subclasses do not register i.e these classes need to register as parallel or not
at the time of instantiation. In Java, we can also load the classes from network and constructed on
runtime. Classloader has a method name defineClass(), which takes an input as a byte array and
loads a class.

Rule of class loader:

One class is loaded in JVM at single point of time.

Summary:
Thus, we learnt, Class loader is used to load Java class files into Java virtual machine and also learnt their
types in Java.

Common questions

Powered by AI

Java Class Loaders consist of three main types: Bootstrap Class Loader, Extensions Class Loader, and System Class Loader. They function hierarchically within the JVM. The Bootstrap Class Loader is the top level and loads core Java classes found in the java.lang and java.util packages as part of the JRE . The Extensions Class Loader is next and loads classes from the 'ext' folders, which can be specified with the java.ext.dirs system property . The System Class Loader is the most familiar to developers as it loads classes specified in the Java classpath . Each class loading task is delegated downward from the higher-level loader to the lower-level loader, adhering to this strict hierarchy for class safety and integrity .

The bootstrap class loader is fundamental to the JVM as it is responsible for loading core Java classes, such as those in java.lang and java.util, which essential applications rely upon . Unlike other class loaders, it is implemented in native code and does not have a parent class loader, making it the root of the class loader hierarchy . Its role ensures that secure and standard classes are loaded in a reliable and consistent manner. By being the initial class loader, it establishes trustworthiness and stability within the JVM’s class loading mechanism, forming the base upon which the class loading hierarchy is built .

The defineClass method in Java's ClassLoader is crucial as it takes an array of bytes and converts it into an instance of Class, allowing dynamic creation and loading of classes during runtime . This process is fundamental in situations where class data is not available in a traditional file system, such as when it's transmitted over a network. For instance, the NetworkClassLoader uses defineClass to create classes from byte arrays downloaded from the network . This method facilitates flexibility and extends the way the Java Virtual Machine loads and utilizes classes, enabling applications to dynamically extend functionality .

Custom implementations like NetworkClassLoader carry significant security implications because they can load classes from potentially untrusted sources, such as over a network . This introduces risks such as unauthorized code execution and exposure to malicious classes. Security managers often utilize class loaders to delineate security domains and control permissions granted to loaded classes . It is crucial for developers to rigorously validate and sanitize input and maintain strict security protocols when employing custom ClassLoader implementations to mitigate these risks .

Binary names are crucial for Java Class Loaders as they serve as identifiers for classes when methods in ClassLoader use them as String parameters . A binary name is fully qualified with the package name and follows a specific syntax outlined by the Java Language Specification. They include examples like 'java.lang.String' and 'javax.swing.JSpinner$DefaultEditor', where nested classes are separated by dollar signs ($). These conventions ensure consistency in class identification and facilitate precise class loading and referencing across different Java environments and modules .

Subclasses of ClassLoader must register as parallel capable to enable concurrent class loading by multiple threads, which prevents deadlocks in non-hierarchical delegation environments . Failure to register could result in these subclasses holding the loader lock during the class loading process, leading to potential deadlocks . Without parallel capability, the JVM would be constrained to loading classes sequentially within a thread, impeding performance and scalability in multithreaded applications .

Java's class loading process exhibits platform-dependence because it requires interaction with the native file system of the host platform to locate and read class files . For example, on UNIX systems, Java loads classes from directories specified by the CLASSPATH environment variable, which is inherently platform-specific . Despite Java's overall platform-independent philosophy, these platform-dependent interactions necessitate adaptation to the file system conventions and environment specifics of the operating system on which the JVM is running .

In the Java ClassLoader delegation model, each class loader has a parent loader. When asked to load a class or resource, a class loader delegates the request to its parent before attempting to resolve it itself . This ensures that classes are loaded uniquely and not duplicated in different class loaders, providing consistency and enabling security; for instance, important system classes are always loaded by the trusted, top-level Bootstrap Class Loader .

Methods and constructors of objects created by a class loader reference other classes through the Java Virtual Machine invoking the loadClass method of the class loader that originally created the class . This delegation is part of the class-loading process, ensuring that dependencies and references within classes are resolved correctly. By leveraging the loadClass method, ClassLoaders can resolve and obtain the appropriate Class instances needed to define method executions and object constructions, inherently supporting the dynamic nature of Java class loading .

Java ClassLoaders need to be parallel capable to avoid potential deadlocks that could occur when classes are loaded concurrently, particularly in environments that do not strictly follow a hierarchical delegation model . Parallel capability allows multiple threads to load classes without blocking each other. A ClassLoader is made parallel capable by registering itself using the ClassLoader.registerAsParallelCapable method during its initialization . Although the default ClassLoader class is registered this way, subclasses must explicitly call this method if they support concurrent loading .

You might also like