0% found this document useful (0 votes)
35 views

Classloader: Instance

The document discusses Java classloaders, including: 1) Classloaders load Java class files into the JVM and are responsible for exceptions like ClassNotFoundException. 2) There is a difference between when a class is "loaded" versus "initialized" - loading makes the class available but initialization performs additional steps. 3) There are three main classloaders in Java - bootstrap, extension, and system classloaders which load core Java classes, extension libraries, and classpath classes respectively.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Classloader: Instance

The document discusses Java classloaders, including: 1) Classloaders load Java class files into the JVM and are responsible for exceptions like ClassNotFoundException. 2) There is a difference between when a class is "loaded" versus "initialized" - loading makes the class available but initialization performs additional steps. 3) There are three main classloaders in Java - bootstrap, extension, and system classloaders which load core Java classes, extension libraries, and classpath classes respectively.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Classloader

Java ClassLoader loads a java class file into JVM.

Like NullPointerException, one exception that is very popular is


ClassNotFoundException. Java class loader is the culprit for this.

When and how a Java class is loaded and initialized?

In Java, you first write a .java file which is then compiled to .class
file.Java is capable of loading classes at run time.

The confusion is what is the difference between “load” and


“initialize”. When and how is a Java class loaded and initialized?

C/C++ is compiled to native machine code first and then it requires a


linking step after compilation. What the linking does is combining
source files from different places and form an executable program.
Java does not do that. The linking-like step for Java is done when
they are loaded into JVM.

The basic principle of java class loading is that classes are only
loaded when needed. Each class that gets loaded may have other classes
that it depends on, so the loading process is recursive.

A lot happens inside the JVM when a class is loaded and initialized,
 Decoding the binary class format,
 Checking compatibility with other classes,
 Verifying the sequence of bytecode operations, and
 Finally constructing a java.lang.Class instance to represent the
new class.

This Class object[java.lang.Class] becomes the basis for all instances


of the new class created by the JVM. It's also the identifier for the
loaded class itself -- you can have multiple copies of the same binary
class loaded in a JVM,each with its own Class instance. Even though
these copies all share the same class name, they will be separate
classes to the JVM.
Types of Java Class Loaders

 Bootstrap Class Loader: Bootstrap class loader loads java’s core


classes from JAVA_HOME/jre/lib/rt.jarlike java.lang, java.util
etc. These are classes that are part of java runtime environment.
Bootstrap class loader don't have any parents, if you call
String.class.getClassLoader() it will return null. Bootstrap
class loader is implemented in native language mostly in C, so
they may differ across different JVMs.
 Extensions Class Loader: JAVA_HOME/jre/lib/ext contains jar
packages that are extensions of standard core java classes.
Extensions class loader loads classes from this ext folder. Using
the system environment propertyjava.ext.dirs you can add ‘ext’
folders and jar files to be loaded using extensions class
loader.Extension ClassLoader in JVM is implemented by
sun.misc.Launcher$ExtClassLoader.
 System(application) Class Loader: Java classes that are
available in the java classpath (and Class-Path attribute of
Manifest file inside JAR) are loaded using System class
loader.Application class loader is a child of Extension
ClassLoader and it’s implemented by
sun.misc.Launcher$AppClassLoader class.

Class loader can load classes from one level above its hierarchy.First
level is bootstrap class loader, second level is extensions
classloader and third level is system class loader.All Java class
loaders except bootstrap, are implemented using java.lang.ClassLoader.

Where to use ClassLoader in Java

 One of the popular example of ClassLoader is AppletClassLoader


which is used to load class by Applet, since Applets are mostly
loaded from internet rather than local file system, By using
separate ClassLoader you can also loads same class from multiple
sources and they will be treated as different class in JVM.
 J2EE uses multiple class loaders to load class from different
location like classes from WAR file will be loaded by Web-app
ClassLoader while classes bundled in EJB-JAR is loaded by another
class loader.
 Some web server also supports hot deploy functionality which is
implemented using ClassLoader.
 You can also use ClassLoader to load classes from database or any
other persistent store.
Class Self Reference

When a java source file is compiled to a binary class, compiler


inserts a field into java class file. It is a public static final
field named ‘class’ of type java.lang.Class.

So for all java classes you can access it as

java.lang.ClassclassObj = ClassName.class;

This Class object contains a method getClassLoader() which returns the


class loader for the class. It will return null if it was loaded by
bootstrap class loader.

How a Java Class Loader Works?

When a class name is given, class loader first locates the class and
then reads a class file of that name from the native file system.

Therefore this loading process is platform dependent.

By default java.lang.ClassLoader is registered as a class loader that


is capable of loading classes in parallel.

But the subclasses needs to register as parallel or not at the time of


instantiation.

Classes can also be loaded from network, constructed on runtime and


loaded. ClassLoader class has a method name defineClass which takes
input as byte array and loads a class.
Class Loader Parent

All class loaders except bootstrap class loader have a parent class
loader. This parent is not as in parent-child relationship of
inheritance. Every class loader instance is associated with a parent
class loader.

When a class loader is entrusted with the responsibility of loading a


class, as a first step it delegates this work to the associated parent
class loader. Then this parent class loader gets the instruction and
sequentially it delegates the call to its parent class loader.

In this chain of hierarchy the bootstrap class loader is at the top.

When a class loader instance is created, using its constructor the


parent classloader can be associated with it.

Class Loader Rule 1

A class is loaded only once into the JVM.

Uniqueness of a class is identified along with the ClassLoader


instance that loaded [this] class into the JVM.

A class is always identified using its fully qualified name


(package.classname).

So when a class is loaded into JVM, you have an entry as (package,


classname, classloader).

Therefore the same class can be loaded twice by two different


ClassLoader instances.
Developers-View

http://zeroturnaround.com/rebellabs/rebel-labs-tutorial-do-you-really-
get-classloaders/#!/

Tour of Java classloading mechanism, both from the JVM and developer
point-of-view.

Something goes wrong with classloading, how would you know to solve
it?

We will look at typical problems related to classloading and how to


solve them.

NoClassDefFoundError, LinkageError and many others are symptoms of


specific things going wrong that you can usually find and fix.

How and why classloaders leak and how can that be remedied.

How to reload a Java class using a dynamic classloader.

We’ll see how objects, classes and classloaders are tied to each other
and the process required to make changes.
java.lang.ClassLoader

It’s important to realize that each classloader is itself an object–an


instance of a class that extends java.lang.ClassLoader.

Every class is loaded by one of those instances and developers are


free to subclass java.lang.ClassLoader to extend the manner in which
the JVM loads classes.

If a classloader has a class and every class is loaded by


aclassloader, then what comes first?

We need an understanding of the mechanics of a classloader (by proxy


of examining its API contract) and the JVM classloader hierarchy.

First, here is the API, with some less relevant parts omitted:

CustomClassLoading

A user class loader gets the chance to load a class before the
primordial class loader does. Because of this, it can load the class
implementation data from some alternate source.

Dynamic ClassLoading
http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
Similar to Custom class loader, class can be loaded dynamically by
extending java.lang.ClassLoader subclass.

Loading a class dynamically is easy. All you need to do is to obtain a


ClassLoader and call its loadClass() method.

Dynamic Class Reloading

Java's builtin Class loaders always checks if a class is already


loaded before loading it. Reloading the class is therefore not
possible using Java's builtin class loaders. To reload a class you
will have to implement your own ClassLoader subclass.

Challenges

Even with a custom subclass of ClassLoader you have a challenge.

Every loaded class needs to be linked. This is done using the


ClassLoader.resolve() method.This method is final, and thus cannot be
overridden in your ClassLoader subclass.

The resolve() method will not allow any given ClassLoader instance to
link the same class twice. Therefore, everytime you want to reload a
class you must use a new instance of your ClassLoader subclass.

Designing Code for Class Reloading

Every class loaded in a Java application is identified by its fully


qualified name (package name + class name), and the ClassLoader
instance that loaded it. That means, that a class MyObject loaded by
class loader A, is not the same class as the MyObject class loaded
with class loader B.

You might also like