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 moduleB
(using classesModuleFinder
,ModuleLayer
, etc). Last one requires standard modulejava.sql
that is not loaded into boot layer with moduleA
. How to load requiredjava.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:
- Have the framework already depend on
java.sql
so that is will be loaded explicitly - 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.