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. I'm using Open JDK 11.0.8. At runtime I get the following exception:
java.lang.ClassNotFoundException: java.sql.Connection
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
I can resolve this by specifying --add-modules java.sql but why do I need to? I don't have this problem with other packages, for example java.beans.PropertyChangeEvent which is in the java.desktop module resolves OK without having to explicitly add that module. Are there other system modules which are not resolved by default?
I have looked at the dependency graph in the JavaDoc for the java.se module, as suggested on a previous question, and java.sql should apparently be there.
To reproduce (this sample code shows the same kind of error as I'm encountering, but the class that cannot be loaded will be SQLException rather than Connection):
For the non-modular code, compile this class and package it into a jar called nomodule-withsql-1.0-SNAPSHOT.jar:
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();
}
}