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. 3.
  2. 6.
  3. 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
  4. 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
  5. 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
  6. 10.

    public class A { void doSmth() { B b =

    new B(); b.doSmthElse(); } } Trivial case
  7. 11.

    public class A { void doSmth() { B b =

    new B(); b.doSmthElse(); } } Trivial case A.class.getClassLoader().loadClass("B");
  8. 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
  9. 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
  10. 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
  11. 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
  12. 20.

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

    | grep MyClass URLClassLoader.getUrls() Container specific logs
  13. 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