How to enter module declaration in JAR?

Suppose I have a lib.jar library for which I do not have source code (or it is written in some non-lingual language that does not yet know about modules). 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 add module-info.class to it.

First I create module-info.java with the following command:

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

Suppose this creates something like this:

 module lib { exports package1; exports package2; } 

Then I try to compile it, but javac fails because 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 the directories package1 and package2 with dummy classes in them, but is there any better approach?

+7
java java-9 module-info java-module
source share
2 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 
+6
source share

As an alternative to compiling the generated module-info.java you also need to extract the contents of the JAR into a directory.

Then compile module-info.java with the source directory (-d) into the directory in which you extracted the content.

Credits: - Alan

+5
source share

All Articles