Java ClassLoaderjava.lang.ClassNotFoundExceptionXuefeng.Wu
明明就在这里啊!
ClassNotFoundException
When Class.forName() findSystemClass() loadClass() NoClassDefFoundError: compile timeRun time
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Key JVM Components
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Default class loadersun.misc.Launcher$ExtClassLoadersun.misc.Launcher$AppClassLoaderCLASSPATH environment variable,-classp...
Tools: jps, jinfo
ClassLoader works
MyClassLoaderDEMO:MyClassLoader
Three principles Delegation principles Visibility Principle Uniqueness PrincipleChild ClassLoader can see class loaded ...
Is it easy?
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Eclipse-OSGi
OSGi Class Loader
Demo Run Plugin_ListAll_Normal Jinfo
Demo Debug Plugin_ListAll_Normal
Demo Debug HelloWorld in remote project
Issue & fix Try to find csdm/client/StartUp.class is exist.
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Maven System Classloader: Classworlds classloadingframework Core Classloader: Plugin Classloaders Custom Classloaders...
Demo: jetty plugin mvn jetty:run @ remote
a limit on how long you can make yourcommand line Isolaed Classloader: your classpathisnt really correct. try to escape t...
Demo: surefire plugin mvn -Dtest=* test
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
SpringString bean = readerBeanClass()Class clz = classLoader.loadClass(bean)clz .newInstance()
Spring ResourceLoader ClassPathXmlApplicationContext DefaultResourceLoader. setClassLoader(ClassLoader classLoader)XmlB...
Issue & fix
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Agenda JVM class and execute Core ClassLoader and MyClassLoader Tools: jps, jinfo Eclipse-OSGi Maven Spring Dynamic...
Other: MethodNotDef Jar version
Class.forName(“...HelloWorld")public class com.carestreamhealth.pas.RemoteWS.utils.HelloWorld {public com.carestreamhealth...
getClassLoader().loadClass(“..HelloWorld")public class com.carestreamhealth.pas.RemoteWS.utils.HelloWorld {public com.care...
new HelloWorldpublic class com.carestreamhealth.pas.RemoteWS.utils.HelloWorld {public com.carestreamhealth.pas.RemoteWS.ut...
Call Site-VM Operation
JVM Spec: 5.3 Creation and Loadinghttp://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
Dynamic Classes and Method (Reflect, Java8) invokedynamichttp://www.slideshare.net/CharlesNutter/jax-2012-invoke-dynamic-...
Java bytecode:Understanding bytecode makes you abetter programmerExample:http://www.ibm.com/developerworks/ibm/library/it-...
Java class loader
Upcoming SlideShare
Loading in...5
×

Java class loader

768

Published on

Java class loader
eclipse, maven,jetty,spring class loader

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
768
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 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); } }}
  • Java class loader

    1. 1. Java ClassLoaderjava.lang.ClassNotFoundExceptionXuefeng.Wu
    2. 2. 明明就在这里啊!
    3. 3. ClassNotFoundException
    4. 4. When Class.forName() findSystemClass() loadClass() NoClassDefFoundError: compile timeRun time
    5. 5. 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
    6. 6. 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
    7. 7. Key JVM Components
    8. 8. 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
    9. 9. Default class loadersun.misc.Launcher$ExtClassLoadersun.misc.Launcher$AppClassLoaderCLASSPATH environment variable,-classpath or -cp command line option,Class-Path attribute of Manifest file inside JAR
    10. 10. Tools: jps, jinfo
    11. 11. ClassLoader works
    12. 12. MyClassLoaderDEMO:MyClassLoader
    13. 13. Three principles Delegation principles Visibility Principle Uniqueness PrincipleChild ClassLoader can see class loaded by Parent ClassLoadbut vice-versa is not true.According to this principle a class loaded by Parentshould not be loaded by Child ClassLoader again.DEMO:demo. ExplicitlyLoadClassByExtension
    14. 14. Is it easy?
    15. 15. 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
    16. 16. Eclipse-OSGi
    17. 17. OSGi Class Loader
    18. 18. Demo Run Plugin_ListAll_Normal Jinfo
    19. 19. Demo Debug Plugin_ListAll_Normal
    20. 20. Demo Debug HelloWorld in remote project
    21. 21. Issue & fix Try to find csdm/client/StartUp.class is exist.
    22. 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. 23. Maven System Classloader: Classworlds classloadingframework Core Classloader: Plugin Classloaders Custom Classloadersboot the classloader graph: ${maven.home}/bootdown the graph contains the core requirements ofMaven:${maven.home}/libeach plugin has its own classloaderthat is a child of Mavens core classloaderplugins/plugin
    24. 24. Demo: jetty plugin mvn jetty:run @ remote
    25. 25. a limit on how long you can make yourcommand line Isolaed Classloader: your classpathisnt really correct. try to escape the confines of anisolated classloader. Manifest-Only JAR:your app may be confused if itfinds only our booter.jar there!
    26. 26. Demo: surefire plugin mvn -Dtest=* test
    27. 27. 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
    28. 28. SpringString bean = readerBeanClass()Class clz = classLoader.loadClass(bean)clz .newInstance()
    29. 29. Spring ResourceLoader ClassPathXmlApplicationContext DefaultResourceLoader. setClassLoader(ClassLoader classLoader)XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader();reader.setBeanClassLoader([Class LoaderB here!]);DefaultListableBeanFactory factory = new DefaultListableBeanFactory(reader);reader.loadBeanDefinitions([xml resource here!]);Default: thread context class loader: Thread.currentThread().getContextClassLoader()
    30. 30. Issue & fix
    31. 31. 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
    32. 32. 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
    33. 33. Other: MethodNotDef Jar version
    34. 34. Class.forName(“...HelloWorld")public class com.carestreamhealth.pas.RemoteWS.utils.HelloWorld {public com.carestreamhealth.pas.RemoteWS.utils.HelloWorld();Code:0: aload_01: invokespecial #8 // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]) throws java.lang.ClassNotFoundException;Code:0: ldc #19 // Stringcom.carestreamhealth.pas.RemoteWS.utils.HelloWorld2: invokestatic #21 // Methodjava/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;5: pop6: return}
    35. 35. getClassLoader().loadClass(“..HelloWorld")public class com.carestreamhealth.pas.RemoteWS.utils.HelloWorld {public com.carestreamhealth.pas.RemoteWS.utils.HelloWorld();Code:0: aload_01: invokespecial #8 // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]) throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException;Code:0: ldc #1 // classcom/carestreamhealth/pas/RemoteWS/utils/HelloWorld2: invokevirtual #23 // Methodjava/lang/Class.getClassLoader:()Ljava/lang/ClassLoader;5: ldc #29 // Stringcom.carestreamhealth.pas.RemoteWS.utils.HelloWorld7: invokevirtual #31 // Methodjava/lang/ClassLoader.loadClass:(Ljava/lang/String;)Ljava/lang/Class;10: pop11: return}
    36. 36. new HelloWorldpublic class com.carestreamhealth.pas.RemoteWS.utils.HelloWorld {public com.carestreamhealth.pas.RemoteWS.utils.HelloWorld();Code:0: aload_01: invokespecial #8 // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: new #1 // class com/carestreamhealth/pas/RemoteWS/utils/HelloWorld3: invokespecial #16 // Method "<init>":()V6: return}
    37. 37. Call Site-VM Operation
    38. 38. JVM Spec: 5.3 Creation and Loadinghttp://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
    39. 39. Dynamic Classes and Method (Reflect, Java8) invokedynamichttp://www.slideshare.net/CharlesNutter/jax-2012-invoke-dynamic-keynote
    40. 40. Java bytecode:Understanding bytecode makes you abetter programmerExample:http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/Java Class:http://en.wikipedia.org/wiki/Java_class_fileJava bytecodes:http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

    ×