Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Everything you need to know about Java Classloaders [Spring IO Barcelona 2015]

Everything you need to know about Java Classloaders [Spring IO Barcelona 2015]

Classloaders: a New Hope.

In the session, we’ll get into a wonderful world where we control exactly where from and what classes does the JVM load. We will design a powerful plugin system that carefully balances flexibility and simplicity. We’ll define classes from bytes, from urls, even from a photo of a rabbit.

But the path to mastery is not a walk in the park. We’ll find mean exception, then even meaner exceptions. Then exceptions whose message doesn’t fit on the screen and is undecipherable. We’ll have to debug everything, challenge assumptions and show that your will is stronger than the soulless stubborness of Tomcat’s classloading scheme.

This session covers the basic functionality and principles of work of Java Classloaders and goes through a series of real world examples to teach how to treat the common issues that custom classloading can create.

Oleg Šelajev

April 30, 2015
Tweet

More Decks by Oleg Šelajev

Other Decks in Programming

Transcript

  1. Oleg Šelajev
    @shelajev
    ZeroTurnaround
    Everything
    you need to know about
    Java Classloaders

    View Slide

  2. whoami
    @shelajev

    View Slide

  3. View Slide

  4. • Basics
    • Practice
    • Problems

    View Slide

  5. What is common?

    View Slide

  6. A class loader is an object that is responsible
    for loading classes.
    Classloader

    View Slide

  7. Separate namespaces for applications
    Each WAR module of EAR gets a classloader
    Class loaders have a parent class loader
    Java EE web module local classes are searched first
    Intentions

    View Slide

  8. Separate namespaces for applications
    Each WAR module of EAR gets a classloader
    Class loaders have a parent class loader
    Java EE web module local classes are searched first
    Intentions

    View Slide

  9. Separate namespaces for applications
    Each WAR module of EAR gets a classloader
    Build a hierarchy of classloaders
    Java EE web module local classes are searched first
    Intentions

    View Slide

  10. public class A {
    void doSmth() {
    B b = new B();
    b.doSmthElse();
    }
    }
    Trivial case

    View Slide

  11. public class A {
    void doSmth() {
    B b = new B();
    b.doSmthElse();
    }
    }
    Trivial case
    A.class.getClassLoader().loadClass("B");

    View Slide

  12. public abstract class Classloader {
    public Class findClass(String name);
    public Class loadClass(String name);
    protected Class defineClass(byte[] b);
    public URL getResource(String name);
    public Enumeration getResources(String name);
    public ClassLoader getParent();
    }
    java.lang.Classloader

    View Slide

  13. public abstract class Classloader {
    public Class findClass(String name);
    public Class loadClass(String name);
    protected Class defineClass(byte[] b);
    public URL getResource(String name);
    public Enumeration getResources(String name);
    public ClassLoader getParent();
    }
    java.lang.Classloader

    View Slide

  14. public abstract class Classloader {
    public Class findClass(String name);
    public Class loadClass(String name);
    protected Class defineClass(byte[] b);
    public URL getResource(String name);
    public Enumeration getResources(String name);
    public ClassLoader getParent();
    }
    java.lang.Classloader

    View Slide

  15. public abstract class Classloader {
    public Class findClass(String name);
    public Class loadClass(String name);
    protected Class defineClass(byte[] b);
    public URL getResource(String name);
    public Enumeration getResources(String name);
    public ClassLoader getParent();
    }
    java.lang.Classloader

    View Slide

  16. • Basics
    • Practice
    • Problems

    View Slide

  17. Plugin Manager

    View Slide

  18. • Basics
    • Practice
    • Problems

    View Slide

  19. ClassNotFoundException
    NoClassDefFoundError
    Not enough classes

    View Slide

  20. IDE class lookup
    find *.jar -exec jar -tf '{}' \; | grep MyClass
    URLClassLoader.getUrls()
    Container specific logs

    View Slide

  21. IncompatibleClassChangeError
    AbstractMethodError
    NoSuch(Method|Field)FoundError
    ClassCastException, IllegalAccessError
    Different classes

    View Slide

  22. -verbose:class
    ClassLoader.getResource()
    javap -private MyClass

    View Slide

  23. LinkageError
    ClassCastException, IllegalAccessError
    Too many classes

    View Slide

  24. -verbose:class
    ClassLoader.getResource()

    View Slide

  25. Each JAR has own class loader
    Class loaders are siblings & central repository
    Explicit imports / exports
    Repository can find relevant class loaders by
    package
    Modern patterns

    View Slide

  26. Validate assumptions
    Don’t write more classloaders :)
    JREBEL rocks!
    Takeaways

    View Slide

  27. [email protected]
    @shelajev
    github.com/zeroturnaround/jf-hw-classloaders
    Contact me

    View Slide

  28. http://0t.ee/jrebel-springio

    View Slide

  29. Classloader leaks

    View Slide

  30. Recreate the object
    Old classloader New classloader
    MyObject.class
    MyObject
    MyObject.class
    MyObject

    View Slide