How to inject module declaration into JAR?

三世轮回 提交于 2019-11-26 11:17:46

问题


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.

I first generate module-info.java with the following command:

jdeps --generate-module-info . lib.jar

Suppose this generated something like that:

module lib {
    exports package1;
    exports 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

Of course, I can create directories package1 and package2 with dummy classes in them, but is there some better approach?


回答1:


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



回答2:


Alternatively, to compile the module-info.java generated you need to also extract the contents of the JAR to a directory.

Then compile the module-info.java with the output directory (-d) set to the directory where you extracted the contents.

Credits :- Alan



来源:https://stackoverflow.com/questions/47222226/how-to-inject-module-declaration-into-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!