Sub topic 12
Services and --bind-services
This chapter assumes you can build a basic jlink image and read a module-info.java file. The question here is why jlink can leave out a module your application loads at run time, and what to do about it.
Why service providers go missing
jlink builds an image by resolving requires directives, starting from the modules you give it with --add-modules. A module that only provides an implementation of a service is not required by anyone, so jlink never reaches it. The application builds and links without errors, and then ServiceLoader finds no providers at run time.
The module system connects services with three pieces:
usesin a consumer module names the service interface it will look up.provides ... within a provider module names the implementation.ServiceLoaderfinds the providers at run time.
A small example
The consumer module defines the service and uses it:
module com.example.app {
exports com.example.app;
uses com.example.app.Greeter;
}package com.example.app;
import java.util.ServiceLoader;
public class Main {
public interface Greeter {
String greet();
}
public static void main(String[] args) {
ServiceLoader.load(Greeter.class)
.forEach(g -> System.out.println(g.greet()));
}
}The provider lives in a second module:
module com.example.greeter.english {
requires com.example.app;
provides com.example.app.Main.Greeter
with com.example.greeter.english.EnglishGreeter;
}package com.example.greeter.english;
import com.example.app.Main.Greeter;
public class EnglishGreeter implements Greeter {
public String greet() {
return "Hello";
}
}The uses line in the consumer module must also name the nested type, so change it to uses com.example.app.Main.Greeter;.
Compile both modules and package them as JARs:
javac -d out --module-source-path src \
$(find src -name '*.java')
mkdir -p mods
jar --create --file mods/app.jar \
--main-class com.example.app.Main \
-C out/com.example.app .
jar --create --file mods/greeter-english.jar \
-C out/com.example.greeter.english .Binding providers into the image
Linking only com.example.app produces an image without the provider:
jlink --module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/plain
build/plain/bin/java -m com.example.appThe program prints nothing. Add --bind-services and jlink also resolves every provider module it can find on the module path for each service the resolved modules uses:
jlink --module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--bind-services \
--output build/bound
build/bound/bin/java -m com.example.appThis image contains com.example.greeter.english, so the program prints Hello.
The cost of binding everything
--bind-services applies to all modules in the image, including JDK modules. The JDK declares many uses directives, so the flag can pull in modules such as jdk.charsets or jdk.localedata, and the image grows noticeably. You have two alternatives:
- Name the providers yourself:
--add-modules com.example.app,com.example.greeter.english. The image stays minimal, but you must keep the list current. - Ask jlink for candidates with
--suggest-providers, for examplejlink --module-path "$JAVA_HOME/jmods:mods" --suggest-providers com.example.app.Main.Greeter. Then add the ones you want.
Exercise
Your image links and starts, but a JDBC driver is never found through DriverManager, even though the driver module is on the module path. What is the likely cause, and what are two ways to fix it?
Show answer
The driver module is a service provider that nothing requires, so jlink left it out. Fix it by adding --bind-services, which includes providers of services that resolved modules use, or by listing the driver module explicitly in --add-modules. The explicit list gives a smaller image.