Save 37% off PRO during our Black Friday Sale! »

Do you really get Classloaders? [Devoxx FR 2014]

Do you really get Classloaders? [Devoxx FR 2014]

Class loaders are at the core of the Java language. Java EE containers, OSGi, NetBeans modules, Grails and many others use class loaders heavily. Yet when something goes wrong, do you know how to solve it? In this session we'll take a tour of the Java class loading mechanism. We will look at typical problems that you get with class loading and how to solve them. ClassNoDefError, IncompatibleClassChangeError, LinkageError and others are symptoms of specific things going wrong that you can usually find and fix. We'll go through a hands on demos of problems with corresponding solutions.

5d01eb7205b787b5991db85a11ee5e68?s=128

Oleg Šelajev

April 18, 2014
Tweet

Transcript

  1. DO YOU REALLY GET CLASSLOADERS? GO OLEG ŠELAJEV @SHELAJEV

  2. BACKGROUND: JREBEL • Hooked into class loading on the JVM

    level • Integrated with the class loading mechanism in more than 20 different servers • Solved hundreds of issues connected to class loading • Learned a lot more about class loaders than we wanted to ☺ www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th flr. Boston, MA, USA, 02116
  3. PLAN • Basics • What is class loading? • How

    was it meant to work? • Problems and solutions • How do class loaders leak? • Modern solutions www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th flr. Boston, MA, USA, 02116
  4. BASICS www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116
  5. JAVA.LANG.CLASSLOADER www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116
  6. EXAMPLE 0 www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116
  7. EXAMPLE 0 www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116 Causes a call to A.class.getClassLoader().loadClass(“B”);
  8. INTENTIONS www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116 • Class loaders have a parent class loader • The parent is usually consulted first • However in a Java EE web module local classes are searched first • Each WAR module of EAR gets a classloader • Separate namespaces for applications
  9. CL HIERARCHY www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116
  10. PROBLEMS www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116
  11. NOT ENOUGH CLASSES www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston

    St., 4th flr. Boston, MA, USA, 02116 •ClassNotFoundException •NoClassDefFoundError
  12. TROUBLESHOOTING www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116 •IDE class lookup (Ctrl+Shift+T in Eclipse) •find *.jar -exec jar -tf '{}' \; | grep MyClass •URLClassLoader.getUrls() •Container specific logs
  13. ALIEN CLASSES www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116 •IncompatibleClassChangeError •AbstractMethodError •NoSuch(Method|Field)FoundError •ClassCastException, IllegalAccessError
  14. TROUBLESHOOTING www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116 •-verbose:class •ClassLoader.getResource() •javap -private MyClass
  15. MORE CLASSES THAN YOU NEED www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199

    545 Boylston St., 4th flr. Boston, MA, USA, 02116 •LinkageError (class loading constraints violated) •ClassCastException, IllegalAccessError
  16. TROUBLESHOOTING www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116 •-verbose:class •ClassLoader.getResource()
  17. www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th flr.

    Boston, MA, USA, 02116 Util3 u = (Util3) Factory3.instanceUntyped(); Factory3.instanceUntyped(); new Util3()
  18. CLASSLOADER LEAKS www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116
  19. CLASSLOADER LEAKS www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116
  20. MODERN PATTERNS www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St.,

    4th flr. Boston, MA, USA, 02116 •Each JAR has own class loader •Class loaders are siblings; + central repository •Explicit imports / exports •Repository can find relevant class loaders by package
  21. TAKEAWAY www.zeroturnaround.com support@zeroturnaround.com 1 (857) 277-1199 545 Boylston St., 4th

    flr. Boston, MA, USA, 02116 •Validate assumptions •Leaking ClassLoader is easy •Isolation works for processes •JREBEL rocks!
  22. QUESTIONS!