Can one Java module export a package whose name is a subpackage of a package from another module? [duplicate]

别来无恙 提交于 2019-12-22 18:49:13

问题


So I know that, in Java 9 modules (Project Jigsaw), split packages are not allowed. That is, the following modules couldn't both export a package with the same name and also be used at the same time at run time:

Module 1

module com.example.foo {
    exports com.example.foo;
}

Module 2

module com.example.foo {
    exports com.example.foo;
}

Not allowed (or, at least, they can't run at the same time). But what isn't clear to me is how subpackages come in to play. If one module exports package com.example.foo, can another package export com.example.foo.bar? For example, I want to do the following:

Module 1

module com.example.foo {
    exports com.example.foo;
    exports com.example.foo.exceptions;
    exports com.example.foo.util;
}

Module 2

module com.example.foo.impl1 {
    requires com.example.foo;
    exports com.example.foo.impl1;
}

Module 3

module com.example.foo.impl2 {
    requires com.example.foo;
    exports com.example.foo.impl2;
}

Is this allowed? Will all three modules be able to be used together at runtime? Or does the fact that module com.example.foo exports com.example.foo preclude another module (com.example.foo.impl1) from exporting a package with a subpackage name (com.example.foo.impl1)?


回答1:


At @RoddyoftheFrozenPeas suggestion, I created a multi-module sample project to demonstrate the behavior here. The tl;dr is that it works! You can, indeed, do this. To prove out that I was also using modules correctly, I tried the first thing that I knew wouldn't work, and I indeed got errors that prevent it from running.

I have created this GitHub gist, where you can see the full source code (I will never delete it), which shows how I set up the project. Underscores in the gist filenames indicate directories (you can't use slashes). The project is laid out as follows:

- root
  - com-example-foo
    - src
      - module-info.java
      - com
        - example
          - foo
            - SalutationProvider.java
  - com-example-foo-impl1
    - src
      - module-info.java
      - com
        - example
          - foo
            - implone
              - StandardOutHelloer.java
  - com-example-foo-impl2
    - src
      - module-info.java
      - com
        - example
          - foo
            - impltwo
              - StandardErrHelloer.java

It compiles fine, and then here is the result of running it:

$ java -Dfile.encoding=UTF-8 -p out/production/com-example-foo-impl1:out/production/com-example-foo -m com.example.foo.implone/com.example.foo.implone.StandardOutHelloer
Hello, World!

$ echo $?
0

$ java -Dfile.encoding=UTF-8 -p out/production/com-example-foo-impl2:out/production/com-example-foo -m com.example.foo.impltwo/com.example.foo.impltwo.StandardErrHelloer
Hello, World!

$ echo $?
15

I believe this should be the answer of the linked duplicate question, because the existing answer just says "there's no such thing as sub-packages" without providing any working examples or documentation that says this is explicitly allowed. However, since the linked duplicate question is, itself, marked as a duplicate of an unrelated question about sub-packages, I can't post this answer there (it's a closed question). As such, I'm posting it here.



来源:https://stackoverflow.com/questions/54370999/can-one-java-module-export-a-package-whose-name-is-a-subpackage-of-a-package-fro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!