Java 9: Possible to have 2 modules with same name on module path

JJ180 :

Is it possible to have 2 modules with the exact same name (but with slightly different contents) on the module path?

As far as I can tell, the Java 9 compiler does not complain about it. I have 2 modules declared as follows:

module com.dj.helper {
    exports com.dj.helper;
}

Both contain the com.dj.helper package but within the package the contents are different. Then in my main application, I am looking to import this module:

module com.dj {
    requires com.dj.helper;
}

Both modules with the same name are on my module path.

I was hoping that when compiling my com.dj module that the compiler would complain about the same module existing twice but it does not. Does this effectively mean you could have 2 versions of the same jar on your module path and Java will not know which one to use?

M Anouti :

JEP 261 of the module system describes the module path as follows:

A module path is a sequence, each element of which is either a module definition or a directory containing module definitions. Each module definition is either

  • A module artifact, i.e., a modular JAR file or a JMOD file containing a compiled module definition, or else

  • An exploded-module directory whose name is, by convention, the module's name and whose content is an "exploded" directory tree corresponding to a package hierarchy.

It then describes the module resolution mechanism:

When searching a module path for a module of a particular name, the module system takes the first definition of a module of that name. Version strings, if present, are ignored; if an element of a module path contains definitions of multiple modules with the same name then resolution fails and the compiler, linker, or virtual machine will report an error and exit. It is the responsibility of build tools and container applications to configure module paths so as to avoid version conflicts; it is not a goal of the module system to address the version-selection problem.

As explained, it means that the compiler will complain only if two modules with the same name exist in the same directory.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=439182&siteId=1