Is there dynamic module discovery in JDK9?
Asked Answered
S

2

7

In the upcoming Java 9, is there a way to dynamically discover which modules are available?

E.g.:

  1. Run some app with some module path
  2. While the app is running, add a few more modules into the module path (e.g. copy into module directories)
  3. From within the app, query "give me all available modules" (perhaps with a filter, such as those providing an alternate implementation of the same service)

I can't quite tell from the documentation.

Shakti answered 2/6, 2017 at 19:6 Comment(0)
C
6

I don’t think such dynamic functionality is supported. Currently, the documentation of ModuleFinder.findAll() states:

A ModuleFinder provides a consistent view of the modules that it locates. If findAll is invoked several times then it will return the same (equals) result each time.

Corby answered 2/6, 2017 at 20:23 Comment(0)
M
6

It depends to which lengths you are willing to go...

By default the module system is hidden quite well from user code and there is no API to tell the module system to reevaluate the given command line (e.g. --module-path, --add-exports, ...). So the immediate answer is "No, you can not change the app by modifying the module path."

What you can do, though, is create a new layer, which contains an entirely new module graph. When launching the JVM, it will create a single layer from the command line flags and the module path content. With the existing API, you can then create new layers on top of that one.

To learn about layers, have a look at The State of the Module System and the Javadoc for ModuleLayer.

Mousy answered 5/6, 2017 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.