Top Banner
@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-
47
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Diving into Java Class Loader

@KonaTechAdda - 6

What is Class Loader ন িঃস্বার্থ ক্লাশ-ল াডার

Md Imran Hasan Hira

Software Engineer

Kona Software Lab Ltd.

http://bd.linkedin.com/in/imranhasanhirahttps://www.linkedin.com/company/kona-software-lab-ltd-

Page 2: Diving into Java Class Loader

Part 1 – Brunch

Part 2 – Buffet Dinner

Java Class Bytecodes JVM

ClassLoading Example Loading Phases

Delegation Model Class Loading exceptions

Page 3: Diving into Java Class Loader

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/classMembers.html

Page 4: Diving into Java Class Loader

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

Page 5: Diving into Java Class Loader

Part-1

What is Java - ন িঃস্বার্থ জাভা

Page 6: Diving into Java Class Loader

What is Java

Page 7: Diving into Java Class Loader

Java iLand

http://vimeo.com/46871479

Page 8: Diving into Java Class Loader

What we know java is

Java

Java Technology

Java Programming

Language

Object Oriented

Write once, Run Everywhere

Page 9: Diving into Java Class Loader

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.

Page 10: Diving into Java Class Loader

How java works

Main.java

Main.cpp Operating System

.classOperating

System

Java Virtual Machine

(JVM)

C/C++ source fileNative executable

.exe

OS executes the machine code

Java source file Java intermediate bytecodes

JVM interprets* the bytecodes

Page 11: Diving into Java Class Loader

Let’s write some java

Welcome.java

Page 12: Diving into Java Class Loader

Compile it

javac Welcome.java

Welcome.java

Compile command

Compile output Welcome.class

Page 13: Diving into Java Class Loader

The Bytecodes (simplified view)

Page 14: Diving into Java Class Loader

Run itWelcome.java

java WelcomeRun command

Page 15: Diving into Java Class Loader

What does JVM do?

bytecodes

Amazing Things

Code Interpretation

Native Instruction mapping Type Conversion

Memory Management

Garbage Collection

Disk/Network access

Security Management

Page 16: Diving into Java Class Loader

There is a specification about What JVM should do

http://docs.oracle.com/javase/specs/

Page 17: Diving into Java Class Loader

Virtual Machines

• Hotspot JVM (OpenJDK)

• Hotspot JVM (Oracle JDK)

• J9 by IBM

• Apache Harmony

• Kaffe OpenVM

• NanoVM

Page 18: Diving into Java Class Loader

An Important part is to load the classes into memory

Page 19: Diving into Java Class Loader

Part-2 Class Loader

Page 20: Diving into Java 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

Page 21: Diving into Java 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

Find in self cache for this class

Put the Class in self cache

Simple Class Loading Procedure (cont.)

Page 22: Diving into Java 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

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.)

Page 23: Diving into Java Class Loader

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

Page 24: Diving into Java 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

Page 25: Diving into Java 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.jarKnown as

Primordial Class Loader

Page 26: Diving into Java 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

Page 27: Diving into Java Class Loader

defineClass()

resolveClass()

findInParentClass()

findLoadedClass()

Put the Class in self cache

Simple class loading procedure

loadClass()

findClass()

Page 28: Diving into Java Class Loader
Page 29: Diving into Java Class Loader

Insight of defineClass() magic

grepcode.com, openJDK 6, ClassLoader.java

Page 30: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

Preparing

Resolving

Initializing

Linking

Page 31: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

Preparing

Resolving

Initializing

Linking

1. Bringing Binary data from a class into

JVM

Page 32: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

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

Page 33: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

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

Page 34: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

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

Page 35: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

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

Page 36: Diving into Java Class Loader

Phases of Class Loading

LoadingVerifying

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

Page 37: Diving into Java Class Loader

Explicit Loading vs Implicit Loading

ClassLoader.loadClass()

Class.forName()

Class A

Class B Class CReference

Inheritance

Instantiation

Explicit Loading

Implicit Loading

Page 38: Diving into Java Class Loader

ClassNotFoundException

NoClassDefFoundException

ClassFormatError

ClassCastException

ClassCircularityError

The Exceptions

Page 39: Diving into Java Class Loader

ClassNotFoundException

• These calls below and the class is not found• Class.forName()

• ClassLoader.findSystemClass()

• ClassLoader.loadClass()

• N.B. Unsuccessful Explicit attempt to load class

Page 40: Diving into Java Class Loader

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

Page 41: Diving into Java Class Loader

ClassCastException

• Casting an object to a subclass of which it is not an instance.• i.e. Integer val = (Integer) new String(“1234”);

• or

Page 42: Diving into Java Class Loader

UnsatisfiedLinkError

• Java Virtual Machine cannot find an appropriate native language definition of a method declared native. i.e.

Page 43: Diving into Java Class Loader

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

Page 44: Diving into Java Class Loader

IncompatibleClassChangeError

ClassFormatError

LinkageError

IllegalAccessError

More…

Page 45: Diving into Java Class Loader

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.

Page 46: Diving into Java Class Loader

Quick Question Answer&

Open Discussion

Page 47: Diving into Java Class Loader

Thank You