NoClassDefFoundError is thrown instead of StackOverflowError

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 11, 17, 21, 25, 26
    • Component/s: 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();
             }
         }
      }

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

              Created:
              Updated:
              Resolved: