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

Classes from java.sql are not loaded when referenced from an automatic module

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Running Windows 10.

      Problem is consistent on Open JDK 15.0.2, Open JDK 11.0.8 and AdoptOpenJDK\jdk-11.0.10.9-openj9

      A DESCRIPTION OF THE PROBLEM :
      I am calling non-modularised code from within a module, by treating the non-modularised as an automatic module. The non-modularised code uses classes from java.sql package. At runtime I get NoClassDefFoundError.

      I can resolve this by specifying --add-modules java.sql but this should not be necessary. This problem does not show up with other packages and modules, for example java.beans.PropertyChangeEvent which is in the java.desktop module resolves OK without having to explicitly add that module.

      The error is:

      Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException
              at nomodule.withsql@1.0-SNAPSHOT/pack1.NeedsJavaSql.m1(NeedsJavaSql.java:8)
              at foo1.project/foo1.Hello.main(Hello.java:5)
      Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
              at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
              at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
              at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the non-modular code sample below and package into a jar called jar called nomodule-withsql-1.0-SNAPSHOT.jar

      Compile the modular code sample and execute the main method, using -p and -m. E.g.,
      java -p target\classes;C:\maven-repository\org\example\nomodule-withsql\1.0-SNAPSHOT\nomodule-withsql-1.0-SNAPSHOT.jar -m foo1.project/foo1.Hello

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A printed stack trace of a dummy exception and a normal program termination
      ACTUAL -
      Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException
              at nomodule.withsql@1.0-SNAPSHOT/pack1.NeedsJavaSql.m1(NeedsJavaSql.java:8)
              at foo1.project/foo1.Hello.main(Hello.java:5)
      Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
              at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
              at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
              at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)

      ---------- BEGIN SOURCE ----------
      For the non-modular code:

          package pack1;
          
          import java.sql.SQLException;
          
          public class NeedsJavaSql
          {
            public static void m1() {
              SQLException e = new SQLException("Whatever");
              e.printStackTrace();
            }
          }

      For the module, here is the module-info:

          module foo1.project {
            exports foo1;
            requires nomodule.withsql;
          }

      Here is the calling class:

          package foo1;
          
          public class Hello {
            public static void main(String... args) {
              pack1.NeedsJavaSql.m1();
            }
          }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Either include a requires java.sql statement in the module-info or include --add-modules java.sql on the command line.

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: