Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jlink with automatic modules

Tags:

I have an explicit modular project which is dependent on an automatic module; e.g. on java.activation. Is it still possible to use jlink?

See this module-info.java:

module hello {     requires java.activation; } 

Then jlink can't add the module:

$ jlink --module-path target/modules --add-modules hello --output target/jlink Error: automatic module cannot be used with jlink: java.activation from file:///C:/Development/jlinkExample/target/modules/javax.activation-api-1.2.0.jar 

From my understanding, an automatic module would contain the whole classpath anyway. So I guess there would be no benefit to creating a runtime image with jlink?

See also: What is an automatic module?

Are there any possibilities to circumvent this issue? Maybe generating a module-info.java for those dependencies?

like image 612
Jonku Avatar asked Sep 26 '18 12:09

Jonku


People also ask

How does jlink work?

jlink is a tool that generates a custom Java runtime image that contains only the platform modules that are required for a given application. Such a runtime image acts exactly like the JRE but contains only the modules we picked and the dependencies they need to function.


1 Answers

From my understanding an automatic module would contain the whole classpath anyway, so I guess there would be no benefit in creating a runtime-image with jlink?

No, automatic modules would not contain the whole classpath. In fact, the artifacts which are not explicitly defined as modules(contain module-info.java) but are found on the modulepath are treated as automatic modules to bridge the gap between explicit modules and the classpath code.

Are there any possibilities to circumvent this issue, maybe generating a module-info for those dependencies?

Yes, you can either create a module-info.java with jdeps or use plugins like moditect to generate module-info for the module and inject it in your existing JAR.

Once the artifact(JAR) has an explicit module declaration, jlink should accept it without failure.

like image 150
Naman Avatar answered Sep 29 '22 23:09

Naman