1

I've seen that in a project with module-info.java, maven adds the modular jars (containing module-info.class or manifest property Automatic-Module-Name ), to the --module-path, and the rest of the jars to the -classpath.

But the spec states:

[...] It does not, however, mean that code in a named module can access types in the unnamed module. A named module cannot, in fact, even declare a dependence upon the unnamed module. This restriction is intentional[...]

From what I know packages from non-modular jars of the classpath are added to the unnamed module.

Since the modular project sources are not allowed to access types from the unnamed module, what is the purpose of adding any jars to the -classpath in the above case?

Marinos An
  • 9,481
  • 6
  • 63
  • 96

1 Answers1

1

It is more subtle. The Apache Maven plugins that need to understand the module-path make use of Plexus Java. This can build up a tree of all the required modules, based on the module descriptors. It understands the module-info file, the Automatic-Module-Name, but also the filename based automatic modules. Ideally nothing should end up on the classpath, it often means you're pulling in too much (transitive) dependencies. However, in some cases jars cannot end up on the module path (filename cannot be transformed to modulename, filename collisions), so you need other tricks. https://openjdk.java.net/jeps/261 shows a list of additonal arguments you can provide to interact between modulepath and classpath/files.

I could have gone for the easy solution (put everything on the modulepath, let javac/java decide what it needs), however for Maven I've decided to go for a clean modulepath: only put jars on the module path that are really used. The rest ends up on the classpath.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • Now it's more clear. I think I missed something initially, which I see now. **Dependencies that are not "required" in `module-info` end up in `-classpath`**. This is happening **even for named modules** (containing `module-info` inside). Is this correct? If yes, my question in title is wrongly expressed. – Marinos An Jul 08 '20 at 11:40
  • Not quite, the important code for this is https://github.com/codehaus-plexus/plexus-languages/blob/master/plexus-java/src/main/java9/org/codehaus/plexus/languages/java/jpms/CmdModuleNameExtractor.java#L81-L94 . Here the jar is being fed to the JDK to get its module name, so it behaves 100% the same. – Robert Scholte Jul 14 '20 at 09:43
  • Good to know this part. For the record, I've also added some test code, which led me to the above conclusion: https://repl.it/@stackit/module-inclasspath-test1#README.md – Marinos An Jul 14 '20 at 10:53