2

How can I list all required modules in Java9?

For system modules, this works:

ModuleFinder.ofSystem()
    .findAll()
    .stream()
    ...

however, not seeing atm how to get a list of the modules that my current code requires.

This question is about querying all the modules programatically. It's not about resolving modularity issues.

igr
  • 9,471
  • 11
  • 60
  • 106

1 Answers1

2

You might want to make use of the example as provided in the Javadoc for ModuleLayer to get a ModuleLayer of your module named let's say one to find out the descriptor and further the set of requires object in its module dependencies.

ModuleFinder finder = ModuleFinder.of(Paths.get("/path/to/module/in/out/production/")); // this is generally a path where I build my modules to

ModuleLayer parent = ModuleLayer.boot();

Configuration cf = parent.configuration()
                         .resolve(finder, ModuleFinder.of(), Set.of("one")); // name of module in module-info

ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithManyLoaders(cf, scl);

layer.modules()
     .stream()
     .map(Module::getDescriptor) // get the descriptor of module
     .map(ModuleDescriptor::requires) // set of requires in the module dependencies
     .forEach(System.out::println);
Naman
  • 21,685
  • 24
  • 196
  • 332