如何将模块声明注入JAR? [英] How to inject module declaration into JAR?
问题描述
假设我有一些库 lib.jar
我没有源代码(或者它是用一些不知道模块的非Java语言编写的)。 lib.jar
没有 module-info.class
,我不想将它用作自动模块,所以我想将 module-info.class
注入其中。
Suppose I have some library lib.jar
for which I do not have the source code (or it is written in some non-Java language which is unaware of modules yet). lib.jar
does not have module-info.class
and I do not want to use it as an automatic module, so I would like to inject module-info.class
into it.
我首先生成 module-info.java
,使用以下命令:
I first generate module-info.java
with the following command:
jdeps --generate-module-info . lib.jar
假设生成类似的东西:
module lib {
exports package1;
exports package2;
}
然后我尝试编译它但是 javac
失败,因为包 package1
和 package2
不存在:
Then I try to compile it but javac
fails because the packages package1
and package2
do not exist:
> javac module-info.java
module-info.java:4: error: package is empty or does not exist: package1
当然,我可以创建目录 package1
和 package2
,其中包含虚拟类,但是有更好的方法吗?
Of course, I can create directories package1
and package2
with dummy classes in them, but is there some better approach?
推荐答案
是的,这可以通过 - 补丁模块实现
选项。此选项最常用于运行时,但它也可以在编译时使用:
Yes, this is possible with the --patch-module
option. This option is most often used at runtime, but it also works at compile time:
javac --patch-module <module name>=<path to jar> module-info.java
这篇关于如何将模块声明注入JAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!