Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject module declaration into JAR?

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?

like image 782
ZhekaKozlov Avatar asked Nov 10 '17 11:11

ZhekaKozlov


People also ask

Can a jar contain multiple modules?

It is that file that defines a module's name, dependencies, and APIs. So there is a strong connection between JARs and modules: JARs are the containers from which the module system creates modules and (at the moment) each JAR can only contain a single module.

Does jar file contain dependencies?

the executable jar should NOT contain the dependencies, just enough of your own code to run, with the dependent classes coming from the before mentioned classpath set up in the manifest.


1 Answers

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
like image 99
ZhekaKozlov Avatar answered Sep 28 '22 08:09

ZhekaKozlov