22

I have three modules module-a, module-b and module-c. When I run my application I get the following:

Error occurred during initialization of boot layer java.lang.module.ResolutionException: Modules module-a and module-b export package some.package to module module-c

What does it mean, taking into consideration that module-c doesn't import some.package and how to fix it?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • So `module-a` and `module-b` export the same package `some.package`. And it seems that `module-c` requiring both `module-a` and `module-b` even withould import of package `some-package` is not allowed. Quite theoretical don't you think? I would consider it a design failure. Though `export ... to ...` or using different versions might help. – Joop Eggen Sep 18 '17 at 10:57
  • 2
    Possible duplicate of [Package conflicts with automatic modules in Java 9](https://stackoverflow.com/questions/42358084/package-conflicts-with-automatic-modules-in-java-9) Not with automatic modules though but the cause seems duplicate of the linked question. – Naman Sep 18 '17 at 12:12

1 Answers1

9

Looks like you've created a split package, meaning two modules (module-a and module-b in your case) contain the same package (some.package). The module system does not allow that. If you place both modules on the module path, you will get this error regardless of whether the package is exported or whether a third module depends on the other two.

The fix is, not to create modules that share the same package. This is not only a technical solution, it also improves the design by making sure each module has a specific and unique API.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • 2
    Seems that it prevents package-level access to methods from the same packages but from different jars. Such way is very popular among libraries and this restriction breaks them all on java 9. – cybersoft Sep 18 '17 at 13:54
  • 2
    They are not broken on Java 9, they work perfectly well on the class path. To be treated as modules, on the other hand, these splits have to be mended. – Nicolai Parlog Sep 18 '17 at 22:54
  • So I can't use old libraries on module path, right? Or can I use both module and class path? – cybersoft Sep 20 '17 at 07:51
  • 1
    You can use module path and class path in parallel (see [this answer](https://stackoverflow.com/a/46289257/2525313)). And you can place old libs on the module path - it's just nor guaranteed that they work there. – Nicolai Parlog Sep 20 '17 at 09:47
  • 2
    I understand that I should not create modules that do not share the same package. But how can I keep up with that if I use external libraries which probably using other libraries I use as well? – Hannes Nov 28 '18 at 21:09
  • Have a look at [`--patch-module`](https://blog.codefx.org/java/five-command-line-options-hack-java-module-system/#Adding-Classes-To-Modules-With--patch-module). For more help, please ask a new question (after searching existing ones). – Nicolai Parlog Nov 30 '18 at 07:09