有一种机制可以将常规 jar 转换为 automatic module从 java-9 开始,通过简单地将它放在 modulepath
而不是 classpath
上。自动模块具有以下属性:
An automatic module is a named module that is defined implicitly, since it does not have a module declaration. Every package in an automatic module is, therefore, considered to be exported even if it might actually be intended only for internal use.
所以自动模块中的所有包都是可用的。但是,如果我们使用 --describe-module
指令检查自动模块,则输出中没有 exports
部分。
例如一个自动模块 main
jar --file main.jar --describe-module
com.foo jar:file:///.../code/module/main.jar/!module-info.class
requires java.base mandated
contains com.foo
如果包 com.foo
无论如何都被认为是导出的,为什么没有 exports
部分?我觉得这有点令人困惑:--describe-module
表明没有没有导出包,但与此同时,main
是一个自动模块,因此所有内容都隐式导出。
最佳答案
我相信,jar
工具中的命令行选项 --describe-module
只是描述 jar 文件的内容,如果它是一个显式模块共享完整的描述符,而对于自动模块,仅描述名称。
引用命令 jar --help
Print the module descriptor, or automatic module name
另一方面,如果你try using jdeps
for generating module-info.java
在给定 jar 文件的自动模块中,您会注意到此类包导出实际上存在于模块声明中。比如说:
jdeps -verbose:class --generate-module-info ../Desktop ~/.m2/repository/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1.jar
写入 .../Desktop/org.apache.commons.lang3/module-info.java
以下
module org.apache.commons.lang3 {
requires transitive java.desktop;
exports org.apache.commons.lang3;
exports org.apache.commons.lang3.arch;
exports org.apache.commons.lang3.builder;
exports org.apache.commons.lang3.concurrent;
exports org.apache.commons.lang3.event;
exports org.apache.commons.lang3.exception;
exports org.apache.commons.lang3.math;
exports org.apache.commons.lang3.mutable;
exports org.apache.commons.lang3.reflect;
exports org.apache.commons.lang3.text;
exports org.apache.commons.lang3.text.translate;
exports org.apache.commons.lang3.time;
exports org.apache.commons.lang3.tuple;
}
关于java - 为什么 Java 自动模块不包含导出部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53290674/