一个Java模块可以从另一个模块导出名称为包子包的包吗? [英] Can one Java module export a package whose name is a subpackage of a package from another module?

查看:281
本文介绍了一个Java模块可以从另一个模块导出名称为包子包的包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我知道在Java 9模块(Project Jigsaw)中,不允许使用拆分包.也就是说,以下模块不能同时导出具有相同名称的包,也不能在运行时同时使用:

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:

模块1

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

模块2

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

不允许(或者至少不能同时运行).但是我不清楚子包是如何发挥作用的.如果一个模块导出软件包com.example.foo,另一模块可以导出com.example.foo.bar吗?例如,我要执行以下操作:

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:

模块1

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

模块2

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

模块3

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

可以吗?这三个模块是否可以在运行时一起使用?还是模块com.example.foo导出com.example.foo的事实阻止了另一个模块(com.example.foo.impl1)导出带有子包名称(com.example.foo.impl1)的包?

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

推荐答案

在@RoddyoftheFrozenPeas建议下,我创建了一个多模块示例项目来在此处演示其行为. tl; dr是它有效!您确实可以做到这一点.为了证明我也在正确使用模块,我尝试了我知道不起作用的第一件事,并且确实遇到了阻止它运行的错误.

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.

我创建了此GitHub要点,您可以在其中查看完整的源代码(我永远不会删除它),它显示了我如何设置项目.要点文件名中的下划线表示目录(不能使用斜杠).该项目的布局如下:

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.

这篇关于一个Java模块可以从另一个模块导出名称为包子包的包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆