Pro Yearly is on sale from $80 to $50! »

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.

5d01eb7205b787b5991db85a11ee5e68?s=128

Oleg Šelajev

April 30, 2015
Tweet

Transcript

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

    Java Classloaders
  2. whoami @shelajev

  3. None
  4. • Basics • Practice • Problems

  5. What is common?

  6. A class loader is an object that is responsible for

    loading classes. Classloader
  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
  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
  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
  10. public class A { void doSmth() { B b =

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

    new B(); b.doSmthElse(); } } Trivial case A.class.getClassLoader().loadClass("B");
  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
  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
  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
  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
  16. • Basics • Practice • Problems

  17. Plugin Manager

  18. • Basics • Practice • Problems

  19. ClassNotFoundException NoClassDefFoundError Not enough classes

  20. IDE class lookup find *.jar -exec jar -tf '{}' \;

    | grep MyClass URLClassLoader.getUrls() Container specific logs
  21. IncompatibleClassChangeError AbstractMethodError NoSuch(Method|Field)FoundError ClassCastException, IllegalAccessError Different classes

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

  23. LinkageError ClassCastException, IllegalAccessError Too many classes

  24. -verbose:class ClassLoader.getResource()

  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
  26. Validate assumptions Don’t write more classloaders :) JREBEL rocks! Takeaways

  27. oleg@zeroturnaround.com @shelajev github.com/zeroturnaround/jf-hw-classloaders Contact me

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

  29. Classloader leaks

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

    MyObject