Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8361093

NoClassDefFoundError is thrown instead of StackOverflowError

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 11, 17, 21, 25, 26
    • core-libs

      When an app creates a custom class loader to load and invoke its own main class, causing recursive re-execution, it throws:

      'Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread “main"'

      This misleading error arises due to secondary class initialisation failures during error reporting, triggered when the JVM attempts to print the original StackOverflowError’s via printStackTrace().

      The behaviour is reproducible as far back as JDK 9, with JDK 8 printing the expected SOE.

      Run the below class to reproduce:

      public class B1903 {
         public static void main(String[] args) {
             try {
                 ClassLoader customLoader = new ClassLoader() { };
                
                 Class<?> clazz = customLoader.loadClass("B1903");
                 clazz.getDeclaredMethod("main", String[].class).invoke(null, (Object) args);
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
      }

            Unassigned Unassigned
            shadowbug Shadow Bug
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: