0

My java application throws ClassNotFoundException, but this exception should be caught in this line java.lang.ClassLoader.loadClass(ClassLoader.java:411).

Due to some reason I can only post part of stack here and I added a java agent to my program.

Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationListener
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:411) 
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 
    at java.lang.Class.getDeclaredMethods(Class.java:1975) 
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) 
    at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:380) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:314) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) 
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:606) 
    ... 28 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 
    at java.lang.Class.getDeclaredMethods(Class.java:1975) 
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) 
    at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:380) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:314) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) 
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:606) 
    ... 28 more
ganle hu
  • 97
  • 8
  • 2
    "but this exception should be caught in this line `java.lang.ClassLoader.loadClass(ClassLoader.java:411)`" ... and why should it? – Tom Sep 28 '19 at 13:20
  • I copy part of the code from ClassLoader.java as follows, ClassNotFoundException explicitly caught here. ```try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader }``` @tom – ganle hu Sep 28 '19 at 13:24
  • Go through the stack trace carefully, it does. " at java.lang.ClassLoader.loadClass(ClassLoader.java:411)" – Martin'sRun Sep 28 '19 at 13:26
  • 1
    The `ClassNotFoundException` that particular try-catch block is catching is thrown by the _parent_ `ClassLoader`. The exception is caught because that means the class was not found by an ancestor, which means it's time for the current `ClassLoader` to try and find the class itself—and if the class cannot be found by the current `ClassLoader` another, different `ClassNotFoundException` is thrown. – Slaw Sep 28 '19 at 13:31
  • Can you provide the run time java version. – Martin'sRun Sep 28 '19 at 13:34
  • java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) @Martin'sRun – ganle hu Sep 28 '19 at 13:43

0 Answers0