The document provides an overview of Java class loaders, detailing their functionality, phases of class loading, and various types of class loading exceptions. It explains the process of loading classes into the Java Virtual Machine (JVM), including the delegation model and the roles of different class loaders. Additionally, it highlights the importance and power of class loaders in dynamically managing class definitions and ensuring security in Java applications.
@KonaTechAdda - 6
Whatis Class Loader
ন িঃস্বার্থ ক্লাশ-ল াডার
Md Imran Hasan Hira
Software Engineer
Kona Software Lab Ltd.
http://bd.linkedin.com/in/imranhasanhira
https://www.linkedin.com/company/kona-software-lab-ltd-
2.
Part 1 –Brunch
Part 2 – Buffet Dinner
Java Class Bytecodes JVM
ClassLoading Example Loading Phases
Delegation Model Class Loading exceptions
3.
References
Title Reference
Demystifying classloading problems, Part 1: An
introduction to class loading and debugging tools
http://www.ibm.com/developerworks/library/j-dclp1/
The basics of Java class loaders http://www.javaworld.com/article/2077260/learn-java/learn-
java-the-basics-of-java-class-loaders.html?null
Java Classloaders Tutorial http://zeroturnaround.com/rebellabs/rebel-labs-tutorial-do-
you-really-get-classloaders/
Core Java Security: Class Loaders, Security
Managers, and Encryption
http://www.informit.com/articles/article.aspx?p=1187967
Java Class Loader - Java Tutorial http://javapapers.com/core-java/java-class-loader/
Inside Class Loaders - O'Reilly Media http://www.onjava.com/pub/a/onjava/2003/11/12/classloader.
html
Discovering Class Members (The Java™ Tutorials >
The Reflection API > Classes)
https://docs.oracle.com/javase/tutorial/reflect/class/classMem
bers.html
4.
Index
• What isJava ? How it runs on PC ?
• Different Java Virtual Machines
• A close look at the bytecodes
• Introduction to class loader
• Building the information from bytecode
• Maintaining the class loading chain
What we knowjava is
Java
Java
Technology
Java
Programming
Language
Object
Oriented
Write once, Run
Everywhere
9.
GreenTalk > Oak> Java
A detail history can be found in http://oracle.com.edgesuite.net/timeline/java/
GreenTalk
• James Gosling initiated a project as Green Team
• Firstly it was called GreenTalk and file extension
was .gt
Oak
• After that it was called Oak and developed as part
of the Green Project
• Oak is a symbol for strength
Java
• Oak renamed to Java for trademark issue with Oak
Technologies
• Java was chosen amongst Silk, Jolt, DNA etc.
10.
How java works
Main.java
Main.cppOperating
System
.class
Operating
System
Java Virtual
Machine
(JVM)
C/C++ source file
Native executable
.exe
OS executes the
machine code
Java source file Java intermediate
bytecodes
JVM interprets* the
bytecodes
Takes classname
(i.e. com.konasl.test.Welcome)
Findthe bytecode data
associated with this classname
Use defineClass() to decode
raw bytecode into Class
Return the Class
Find in self cache for this class
Put the Class in self cache
Simple Class Loading Procedure (cont.)
22.
Takes classname
(i.e. com.konasl.test.Welcome)
Findthe bytecode data
associated with this classname
Use defineClass() to decode
raw bytecode into Class
Use resolveClass() to resolve
the other referenced classes
Return the Class
Find in super classloaders for
this class
Find in self cache for this class
Put the Class in self cache
Simple Class Loading Procedure (cont.)
23.
Class Loader chain
•Bootstrap Class Loader
• <JAVA_HOME>/jre/lib
• Part of the core JVM, written in native code
• Extension Class Loader
• <JAVA_HOME>/jre/lib/ext
• java.ext.dirs.
• Implemented by sun.misc.Launcher$ExtClassLoader
• System Class Loader
• <CLASSPATH>
• java.class.path
• Implemented by the sun.misc.Launcher$AppClassLoader
24.
Class Loader DelegationModel
Bootstrap
Class Loader
Extension
Class Loader
System Class
Loader
User Defined
Class Loader
User Defined
Class Loader
User Defined
Class Loader
25.
Class Loader DelegationModel
Bootstrap
Class Loader
Extension
Class Loader
System Class
Loader
User Defined
Class Loader
User Defined
Class Loader
User Defined
Class Loader
$JAVAHOME/jre/lib/rt.jar
Known as
Primordial
Class Loader
26.
Class Loader DelegationModel
Bootstrap
Class Loader
Extension
Class Loader
System Class
Loader
User Defined
Class Loader
User Defined
Class Loader
User Defined
Class Loader
$JAVAHOME/jre/lib/rt.jar
$JAVAHOME/jre/lib/ext/*.jar
$CLASSPATH
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
31.
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
32.
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
2. Incorporating the binary data
into the runtime state of the JVM
33.
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
2. Incorporating the binary data
into the runtime state of the JVM
2.1 Ensure class is properly
formed and fit for use by the
JVM
34.
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
2. Incorporating the binary data
into the runtime state of the JVM
2.1 Ensure class is properly
formed and fit for use by the
JVM
2.2 Allocating memory
needed by the Class
35.
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
2. Incorporating the binary data
into the runtime state of the JVM
2.1 Ensure class is properly
formed and fit for use by the
JVM
2.2 Allocating memory
needed by the Class 3. Class variables are given
their proper initial values
36.
Phases of ClassLoading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
2. Incorporating the binary data
into the runtime state of the JVM
2.1 Ensure class is properly
formed and fit for use by the
JVM
2.2 Allocating memory
needed by the Class
2.3 Transforming symbolic
references in the constant
pool, into direct references
3. Class variables are given
their proper initial values
37.
Explicit Loading vsImplicit Loading
ClassLoader.loadClass()
Class.forName()
Class A
Class B Class C
Reference
Inheritance
Instantiation
Explicit Loading
Implicit Loading
…
…
ClassNotFoundException
• These callsbelow and the class is not found
• Class.forName()
• ClassLoader.findSystemClass()
• ClassLoader.loadClass()
• N.B. Unsuccessful Explicit attempt to load class
40.
NoClassDefFoundException
• JVM orCL instance tries to load class and the class is not found while
• in regular method call
• creating a new instance using the ‘new’
• N.B. Unsuccessful Implicit class load
41.
ClassCastException
• Casting anobject to a subclass of which it is not an instance.
• i.e. Integer val = (Integer) new String(“1234”);
• or
ClassCircularityError
•Java Virtual Machinecannot find an appropriate
native language definition of a method declared
native. i.e.
Class A
Class B extends A
Class B
Class A extends B
A.class B.class A.class B.class
B.class A.classTry to load this two classes
Power of ClassLoader
•Dynamically loading desired classes
•Loading different versions of the classes
•Generating new class definitions on the fly
•Verify custom code signature before executing
code
•Use encrypted* bytecodes in program
Most of the programs don’t need to mess with Class Loader.