Issue
For debugging I have a need to see the actual modulepath. How can I print it out at runtime?
The problem: Using ServiceLoader to load a module against a defined API works fine in normal runtime environment but not in testing. I'd got to find out why.
As I am new to ServiceLoader, it may not to be enough that a provider module can be found on modulepath. However, my first question is: is it on modulepath even in test environment?
Solution
When you want to check the presence of a module, you can straight-forwardly check the presence of a module, e.g.
String moduleName = "java.compiler";
ModuleLayer.boot().findModule(moduleName)
.ifPresentOrElse(
m -> System.out.println(m + " loaded via " + m.getClassLoader()),
() -> System.out.println(moduleName + " not found"));
When the module is not present, you can check module path using the non-standard system property,
System.out.println(System.getProperty("jdk.module.path"));
Besides possibility of a mismatching path, it’s worth checking the context loader of each environment if loading a service fails in one environment, as the method ServiceLoader.load(Class)
uses the context loader for searching.
Also check that the module’s classes are not accidentally provided on the old class path.
Answered By - Holger
Answer Checked By - Gilberto Lyons (JavaFixing Admin)