SlideShare a Scribd company logo
1 of 47
@KonaTechAdda - 6
What is 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-
Part 1 – Brunch
Part 2 – Buffet Dinner
Java Class Bytecodes JVM
ClassLoading Example Loading Phases
Delegation Model Class Loading exceptions
References
Title Reference
Demystifying class loading 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
Index
• What is Java ? 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
Part-1
What is Java
- ন িঃস্বার্থ জাভা
What is Java
Java iLand
http://vimeo.com/46871479
What we know java is
Java
Java
Technology
Java
Programming
Language
Object
Oriented
Write once, Run
Everywhere
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.
How java works
Main.java
Main.cpp Operating
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
Let’s write some java
Welcome.java
Compile it
javac Welcome.java
Welcome.java
Compile command
Compile output Welcome.class
The Bytecodes (simplified view)
Run it
Welcome.java
java WelcomeRun command
What does JVM do?
bytecodes
Amazing
Things
Code Interpretation
Native Instruction mapping Type Conversion
Memory Management
Garbage Collection
Disk/Network access
Security Management
There is a specification about What JVM
should do
http://docs.oracle.com/javase/specs/
Virtual Machines
• Hotspot JVM (OpenJDK)
• Hotspot JVM (Oracle JDK)
• J9 by IBM
• Apache Harmony
• Kaffe OpenVM
• NanoVM
…
An Important part is to load the classes into
memory
Part-2
Class Loader
Takes classname
(i.e. com.konasl.test.Welcome)
Find the bytecode data
associated with this classname
Use defineClass() to decode
raw bytecode into Class
Return the Class
Simple Class Loading Procedure
Takes classname
(i.e. com.konasl.test.Welcome)
Find the 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.)
Takes classname
(i.e. com.konasl.test.Welcome)
Find the 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.)
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
Class Loader Delegation Model
Bootstrap
Class Loader
Extension
Class Loader
System Class
Loader
User Defined
Class Loader
User Defined
Class Loader
User Defined
Class Loader
Class Loader Delegation Model
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
Class Loader Delegation Model
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
defineClass()
resolveClass()
findInParentClass()
findLoadedClass()
Put the Class in self cache
Simple class loading procedure
loadClass()
findClass()
Insight of defineClass() magic
grepcode.com, openJDK 6, ClassLoader.java
Phases of Class Loading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
Phases of Class Loading
Loading
Verifying
Preparing
Resolving
Initializing
Linking
1. Bringing Binary
data from a class into
JVM
Phases of Class Loading
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
Phases of Class Loading
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
Phases of Class Loading
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
Phases of Class Loading
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
Phases of Class Loading
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
Explicit Loading vs Implicit Loading
ClassLoader.loadClass()
Class.forName()
Class A
Class B Class C
Reference
Inheritance
Instantiation
Explicit Loading
Implicit Loading
…
…
ClassNotFoundException
NoClassDefFoundException
ClassFormatError
ClassCastException
ClassCircularityError
The Exceptions
ClassNotFoundException
• These calls below and the class is not found
• Class.forName()
• ClassLoader.findSystemClass()
• ClassLoader.loadClass()
• N.B. Unsuccessful Explicit attempt to load class
NoClassDefFoundException
• JVM or CL 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
ClassCastException
• Casting an object to a subclass of which it is not an instance.
• i.e. Integer val = (Integer) new String(“1234”);
• or
UnsatisfiedLinkError
•Java Virtual Machine cannot find an appropriate
native language definition of a method declared
native. i.e.
ClassCircularityError
•Java Virtual Machine cannot 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
IncompatibleClassChangeError
ClassFormatError
LinkageError
IllegalAccessError
More…
Power of Class Loader
•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.
Quick Question Answer
&
Open Discussion
Thank You 

More Related Content

What's hot

Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introductionRasheed Waraich
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsMahika Tutorials
 
Introduction to java
Introduction to java Introduction to java
Introduction to java Java Lover
 
JRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAJRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAMehak Tawakley
 
Java 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeJava 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeSimone Bordet
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Edureka!
 
Introduction to java
Introduction to java Introduction to java
Introduction to java Sandeep Rawat
 
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
LinkedList vs Arraylist- an in depth look at java.util.LinkedListLinkedList vs Arraylist- an in depth look at java.util.LinkedList
LinkedList vs Arraylist- an in depth look at java.util.LinkedListMarcus Biel
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous JavascriptGarrett Welson
 
Spring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsSpring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsGuy Nir
 

What's hot (20)

Jdk,jre,jvm
Jdk,jre,jvmJdk,jre,jvm
Jdk,jre,jvm
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Spring boot
Spring bootSpring boot
Spring boot
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
JRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAJRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVA
 
Java 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeJava 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgrade
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
 
Core java
Core java Core java
Core java
 
Core java
Core javaCore java
Core java
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
LinkedList vs Arraylist- an in depth look at java.util.LinkedListLinkedList vs Arraylist- an in depth look at java.util.LinkedList
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
core java
core javacore java
core java
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
Spring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsSpring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topics
 

Similar to Diving into Java Class Loader

Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Java class loading  tips and tricks - Java Colombo Meetup, January, 2014Java class loading  tips and tricks - Java Colombo Meetup, January, 2014
Java class loading tips and tricks - Java Colombo Meetup, January, 2014Sameera Jayasoma
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers GuideDaisyWatson5
 
How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDEShweta Oza
 
Class loader basic
Class loader basicClass loader basic
Class loader basic명철 강
 
5 the final_hard_part
5 the final_hard_part5 the final_hard_part
5 the final_hard_partHonnix Liang
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
Java class loader
Java class loaderJava class loader
Java class loaderbenewu
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
java_bba_21_vision academy_final.pdf
java_bba_21_vision academy_final.pdfjava_bba_21_vision academy_final.pdf
java_bba_21_vision academy_final.pdfakankshasorate1
 
Do you really get class loaders?
Do you really get class loaders? Do you really get class loaders?
Do you really get class loaders? guestd56374
 
A begineers guide of JAVA - Getting Started
 A begineers guide of JAVA - Getting Started A begineers guide of JAVA - Getting Started
A begineers guide of JAVA - Getting StartedRakesh Madugula
 

Similar to Diving into Java Class Loader (20)

Java Class Loading
Java Class LoadingJava Class Loading
Java Class Loading
 
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Java class loading  tips and tricks - Java Colombo Meetup, January, 2014Java class loading  tips and tricks - Java Colombo Meetup, January, 2014
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers Guide
 
How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDE
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
5 the final_hard_part
5 the final_hard_part5 the final_hard_part
5 the final_hard_part
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
Java class loader
Java class loaderJava class loader
Java class loader
 
testing ppt
testing ppttesting ppt
testing ppt
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Java bcs 21_vision academy_final
Java bcs 21_vision academy_finalJava bcs 21_vision academy_final
Java bcs 21_vision academy_final
 
java_bba_21_vision academy_final.pdf
java_bba_21_vision academy_final.pdfjava_bba_21_vision academy_final.pdf
java_bba_21_vision academy_final.pdf
 
Do you really get class loaders?
Do you really get class loaders? Do you really get class loaders?
Do you really get class loaders?
 
Class loaders
Class loadersClass loaders
Class loaders
 
A begineers guide of JAVA - Getting Started
 A begineers guide of JAVA - Getting Started A begineers guide of JAVA - Getting Started
A begineers guide of JAVA - Getting Started
 

More from Md Imran Hasan Hira

More from Md Imran Hasan Hira (6)

System design basics - Part 2
System design basics - Part 2System design basics - Part 2
System design basics - Part 2
 
System design basics - Part 1
System design basics - Part 1System design basics - Part 1
System design basics - Part 1
 
Pursuit of success
Pursuit of successPursuit of success
Pursuit of success
 
Linkedin Guide
Linkedin GuideLinkedin Guide
Linkedin Guide
 
Java Reflection @KonaTechAdda
Java Reflection @KonaTechAddaJava Reflection @KonaTechAdda
Java Reflection @KonaTechAdda
 
UX Design - Think about it !
UX Design - Think about it !UX Design - Think about it !
UX Design - Think about it !
 

Recently uploaded

WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 

Recently uploaded (20)

WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 

Diving into Java Class Loader

  • 1. @KonaTechAdda - 6 What is 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 class loading 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 is Java ? 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
  • 5. Part-1 What is Java - ন িঃস্বার্থ জাভা
  • 8. What we know java 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.cpp Operating 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
  • 11. Let’s write some java Welcome.java
  • 12. Compile it javac Welcome.java Welcome.java Compile command Compile output Welcome.class
  • 15. What does JVM do? bytecodes Amazing Things Code Interpretation Native Instruction mapping Type Conversion Memory Management Garbage Collection Disk/Network access Security Management
  • 16. There is a specification about What JVM should do http://docs.oracle.com/javase/specs/
  • 17. Virtual Machines • Hotspot JVM (OpenJDK) • Hotspot JVM (Oracle JDK) • J9 by IBM • Apache Harmony • Kaffe OpenVM • NanoVM …
  • 18. An Important part is to load the classes into memory
  • 20. Takes classname (i.e. com.konasl.test.Welcome) Find the bytecode data associated with this classname Use defineClass() to decode raw bytecode into Class Return the Class Simple Class Loading Procedure
  • 21. Takes classname (i.e. com.konasl.test.Welcome) Find the 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) Find the 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 Delegation Model Bootstrap Class Loader Extension Class Loader System Class Loader User Defined Class Loader User Defined Class Loader User Defined Class Loader
  • 25. Class Loader Delegation Model 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 Delegation Model 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
  • 27. defineClass() resolveClass() findInParentClass() findLoadedClass() Put the Class in self cache Simple class loading procedure loadClass() findClass()
  • 28.
  • 29. Insight of defineClass() magic grepcode.com, openJDK 6, ClassLoader.java
  • 30. Phases of Class Loading Loading Verifying Preparing Resolving Initializing Linking
  • 31. Phases of Class Loading Loading Verifying Preparing Resolving Initializing Linking 1. Bringing Binary data from a class into JVM
  • 32. Phases of Class Loading 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 Class Loading 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 Class Loading 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 Class Loading 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 Class Loading 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 vs Implicit Loading ClassLoader.loadClass() Class.forName() Class A Class B Class C Reference Inheritance Instantiation Explicit Loading Implicit Loading … …
  • 39. ClassNotFoundException • These calls below and the class is not found • Class.forName() • ClassLoader.findSystemClass() • ClassLoader.loadClass() • N.B. Unsuccessful Explicit attempt to load class
  • 40. NoClassDefFoundException • JVM or CL 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 an object to a subclass of which it is not an instance. • i.e. Integer val = (Integer) new String(“1234”); • or
  • 42. UnsatisfiedLinkError •Java Virtual Machine cannot find an appropriate native language definition of a method declared native. i.e.
  • 43. ClassCircularityError •Java Virtual Machine cannot 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
  • 45. Power of Class Loader •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.