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

…衆ロ難τιáo~ 提交于 2019-12-06 11:10:33

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.

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