2

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();
  }
}
1
  • 2
    A couple of points to clarify: I get the same behaviour is also seen on Open JDK 15.0.2 and Eclipse OpenJ9 Further research - the java.se module is not relevant anyway to this situation - see bugs.java.com/bugdatabase/view_bug.do?bug_id=8205169 The basic question remains why automatically resolves java.desktop and does not automatically resolve java.sql Commented Feb 16, 2021 at 11:46

1 Answer 1

1

The answer is that although the java.base module, which is available by default, does not require any other module, it does bind to services provided by other modules, so that makes them available by default, and then any other modules they require are in turn made available, and an automatic module can use any of them. So if you look at the dependency graph for the java.base module it is completely empty, but actually in practice it drags in 37 modules, including java.desktop.

However, java.sql is not one of those 37 modules.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but where is this documented?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.