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

Interrupting class loading from an exploded module causes CNFE to be thrown

    Details

      Description

      FULL PRODUCT VERSION :
      java version "9.0.4"
      Java(TM) SE Runtime Environment (build 9.0.4+11)
      Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.16299.248]

      A DESCRIPTION OF THE PROBLEM :
      Let Thread-X be a thread that is registered as a shutdown hook and suppose that the Thread-X has started the initialization of a class B. Then the initialization fails and NoClassDefFoundError is thrown if the following conditions hold:
        * Thread-X is interrupted at the time of the initialization of the class-B.
        * Class-B belongs to a named module.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I describe the case with the following sample code.
      Let SRC/ is a module source path.
      Make SRC/mainmodule/module-info.java as:
      -------------
      module mainmodule{
      exports mainpackage;
      }
                  -------------
      Make SRC/mainmodule/mainpackage/Main.java as:
      -------------
      package mainpackage;

      public class Main{
      public static void main(String... args) throws Exception {
      Runtime.getRuntime().addShutdownHook(
      new Thread(()->{
      Thread.currentThread().interrupt();
      B.run(); // this line throws an Error.
      System.out.println("i want this code to be executed");
      })
      );
      }
      }
                  -------------
      Make SRC/mainmodule/mainpackage/B.java as:
      -------------
      package mainpackage;

      public class B{
      public static void run(){}
      }
                  -------------
      Compile all of them by:
         javac --module-source-path SRC -d DST --module mainmodule
      Execute Main#main by:
         java --module-path DST --add-modules mainmodule mainpackage.Main

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      System.out.println("i want this code to be executed"); is executed and the message is printed in System.out.
      ACTUAL -
      NoClassDefFoundError occurs and the stack trace is printed in System.err.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "Thread-0" java.lang.NoClassDefFoundError: mainpackage/B
              at mainmodule/mainpackage.Main.lambda$main$0(Main.java:8)
              at java.base/java.lang.Thread.run(Unknown Source)
      Caused by: java.lang.ClassNotFoundException: mainpackage.B
              at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
              at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
              at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
              ... 2 more

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      pasted above.
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              alanb Alan Bateman
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: