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

后端 未结 2 1149
一生所求
一生所求 2020-12-30 09:42

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 n

2条回答
  •  遥遥无期
    2020-12-30 10:09

    No

    It is not possible to have two modules of the same name in the same directory on the module path. The official documentation is not putting that information in a particularly prominent spot - it's the Javadoc of ModuleFinder::of that gives it away:

    It is an error if a directory contains more than one module with the same name.

    I've created a small demo project for the module system and it covers that case by creating two versions of the same module...

    jar --create
        --file mods/monitor.observer.beta-1.0.jar
        --module-version 1.0
        -C classes/monitor.observer.beta .
    jar --create
        --file mods/monitor.observer.beta-2.0.jar
        --module-version 2.0
        -C classes/monitor.observer.beta .
    

    ... and then referencing the folder in the next compilation ...

    javac
        --module-path mods
        -d classes/monitor.statistics
        $(find monitor.statistics -name '*.java')
    

    ... which as expected leads to the following error message:

    error: duplicate module on application module path
    module in monitor.observer.beta
    1 error
    

    Note that I said in the same directory. Across directories multiple modules are possible.

    Yes

    The module system only enforces uniqueness within directories. Once again from ModuleFinder::of (emphasis mine):

    The module finder locates modules by searching each directory, exploded module, or packaged module in array index order. It finds the first occurrence of a module with a given name and ignores other modules of that name that appear later in the sequence.

    That makes it possible to have the same module in different directories.

提交回复
热议问题