Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 525
Nick Williams Avatar asked Jan 29 '26 23:01

Nick Williams


1 Answers

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.

like image 72
Nick Williams Avatar answered Feb 01 '26 11:02

Nick Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!