2

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)?

Nick Williams
  • 2,864
  • 5
  • 29
  • 43
  • 2
    When i have question like this i just try it and see if it works.... :) – Roddy of the Frozen Peas Jan 25 '19 at 18:31
  • 1
    Indeed. I didn't see that question after searching for about an hour, and I could see an argument for this being a duplicate of that. On the other hand, that question does not really have what I would consider an answer. What the alleged answer says _sounds_ right, and it was what I thought as well, but I would sure like to see some kind of authoritative documentation on this matter. – Nick Williams Jan 25 '19 at 19:01
  • 1
    As Roddy says, I may have to just try it out. Unfortunately, creating a multi-module "hello world" (for lack of a better description) project is far less trivial than a traditional single-module/pre-module "hello world." It would actually take a bit of time, and I thought asking here might be quicker. – Nick Williams Jan 25 '19 at 19:02
  • 1
    @RoddyoftheFrozenPeas and Alexey, I have created a real, working example and posted an answer below. – Nick Williams Jan 25 '19 at 19:58

1 Answers1

2

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.

Nick Williams
  • 2,864
  • 5
  • 29
  • 43
  • 1
    I've reopened the other question. Delete this and post it there. The solution to questions being closed is **not** to create an exact duplicate... That just makes it harder to find information because it's split across multiple questions. – Michael Jan 26 '19 at 10:25
  • So, yes or no, is it allowed? This answer does not explicitly actually answer the question. – Jesse Feb 19 '20 at 11:28