1

I am facing a similar situation as this post, which includes a demonstrating example of the problem that occurs when trying to dynamically load a Java 9-modularized jar, which depends on a JDK module that is not loaded by default. Since I cannot phrase it any better, here is an excerpt of the main idea:

Suppose we have module A that dynamically loads module B (using classes ModuleFinder, ModuleLayer, etc). Last one requires standard module java.sql that is not loaded into boot layer with module A. How to load required java.sql from JDK (or JRE) using java code?

Specifically, my plugin (module B in the generic scenario) tries to create a connection to a Mysql database. On its own, it work as expected, but when I initially tried to use it in the framework (module A in the generic scenario) , it failed for the same reason as in the linked topic:

java.lang.module.FindException: Module java.sql not found, required by app.module.sql

If my understanding is correct, there is no way to load modules from JDK on runtime (unlike third party modules). Therefore, the two option I found are:

  1. Have the framework already depend on java.sql so that is will be loaded explicitly
  2. Use --add-modules ALL-SYSTEM, which I prefer for not failing for other JDK modules in the future. However, I read that this has a drawback of loading too many modules, potentially two modules with the same name causing trouble.

However, this does not solve everything: For some reason, the automatic loading of the mysql database driver hidden behind the DriverManager.getConnection method, also does not work. I need to explicitly call Class.forName("com.mysql.cj.jdbc.Driver") beforehand.

So I wonder, if there is a better way by now (the post is four years old) or if the option two is fine for what it's worth.

rheinert.leon
  • 69
  • 1
  • 6
  • 1
    Either `Thread.setContextClassLoader` or `ServiceLoader.load(moduleLayer, javax.sql.Driver.class).forEach(d -> {})` may help. – Johannes Kuhn Jun 22 '23 at 22:51

0 Answers0