This document discusses Java classloaders and classloading in Java. It covers the core classloader hierarchy in Java including the bootstrap, extension, and system classloaders. It also discusses how tools like jps and jinfo can be used to view classloading information. Additional topics covered include classloading in OSGi, Maven, Spring, dynamic classloading using reflection, and causes of ClassNotFoundExceptions. The presentation provides examples and demos of different classloading scenarios.
13. Three principles
Delegation principles
Visibility Principle
Uniqueness Principle
Child ClassLoader can see class loaded by Parent ClassLoad
but vice-versa is not true.
According to this principle a class loaded by Parent
should not be loaded by Child ClassLoader again.
DEMO:demo. ExplicitlyLoadClassByExtension
21. Issue & fix
Try to find csdm/client/StartUp.class is exist.
22. Agenda
JVM class and execute
Core ClassLoader and MyClassLoader
Tools: jps, jinfo
Eclipse-OSGi
Maven
Spring
Dynamic Classes and Method (Reflect, Java 8)
Other: MethodNotDef
23. Maven
System Classloader: Classworlds classloading
framework
Core Classloader:
Plugin Classloaders
Custom Classloaders
boot the classloader graph: ${maven.home}/boot
down the graph contains the core requirements of
Maven:
${maven.home}/lib
each plugin has its own classloader
that is a child of Maven's core classloader
plugins/plugin
25. a limit on how long you can make your
command line
Isolaed Classloader: your classpath
isn't really correct. try to escape the confines of an
isolated classloader.
Manifest-Only JAR:your app may be confused if it
finds only our booter.jar there!
40. Java bytecode:
Understanding bytecode makes you a
better programmer
Example:
http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
Java Class:
http://en.wikipedia.org/wiki/Java_class_file
Java bytecodes:
http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings
Notas del editor
package demo;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;/** * Created with IntelliJ IDEA. * User: 19002850 * Date: 13-5-20 * Time: 下午4:35 * To change this template use File | Settings | File Templates. */public class MyClassLoader extends ClassLoader { private static final int BUFFER_SIZE = 8192; protected synchronized Class loadClass(String className, boolean resolve) throws ClassNotFoundException { log("Loading class: " + className + ", resolve: " + resolve); // 1. is this class already loaded? Class cls = findLoadedClass(className); if (cls != null) { return cls; } // 2. get class file name from class name String clsFile = className.replace('.', '/') + ".class"; // 3. get bytes for class byte[] classBytes = null; try {InputStream in = getResourceAsStream(clsFile); byte[] buffer = new byte[BUFFER_SIZE];ByteArrayOutputStream out = new ByteArrayOutputStream();int n = -1; while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) {out.write(buffer, 0, n); }classBytes = out.toByteArray(); } catch (IOException e) { log("ERROR loading class file: " + e); } if (classBytes == null) { throw new ClassNotFoundException("Cannot load class: " + className); } // 4. turn the byte array into a Class try {cls = defineClass(className, classBytes, 0, classBytes.length); if (resolve) {resolveClass(cls); } } catch (SecurityException e) { // loading core java classes such as java.lang.String // is prohibited, throws java.lang.SecurityException. // delegate to parent if not allowed to load classcls = super.loadClass(className, resolve); } return cls; } private static void log(String s) {System.out.println(s); }}
package demo;import java.util.logging.Level;import java.util.logging.Logger;/** * Java program to demonstrate How ClassLoader works in Java, * in particular about visibility principle of ClassLoader. * * @author Javin Paul */public class ExplicitlyLoadClassByExtension { public static void main(String args[]) { try { //printing ClassLoader of this classSystem.out.println("-------------");System.out.println("ExplicitlyLoadClassByExtension.getClass().getClassLoader() : " + ExplicitlyLoadClassByExtension.class.getClassLoader()); //trying to explicitly load this class again using Extension class loaderSystem.out.println("ExplicitlyLoadClassByExtension.getClass().getClassLoader().getParent() : " + ExplicitlyLoadClassByExtension.class.getClassLoader().getParent());Class.forName("demo.ExplicitlyLoadClassByExtension", true , ExplicitlyLoadClassByExtension.class.getClassLoader().getParent()); } catch (ClassNotFoundException ex) {Logger.getLogger(ExplicitlyLoadClassByExtension.class.getName()).log(Level.SEVERE, null, ex); } }}