The Java Platform Module System (JPMS) allows a module to declare an optional dependency using requires static in the module-info.java file:
module my.module {
requires static some.optional.module;
}
However, how should you check in your code whether the module is present at runtime?
The Project Jigsaw: Optional Modules page proposed:
public boolean isModulePresent(String mn);
But, it appears that was dropped because there is no such method in the latest Java releases. Neither am I able to find the @RequireOptionalModule annotation mentioned there in the current Java version.
This example on blog@CodeFX suggests a quite verbose method chain on StackWalker (which is explained here).
Edit: That example tries to find out if a module is available to the caller class, which is not necessary in my case.
Is there an easier way to perform this check, or can a simple Class.forName check suffice if the presence of a certain class indicates that a module is present?