Sub topic 12
Services and --bind-services
This chapter assumes you can write a module-info.java, run jdeps, and build a basic image with jlink. It covers why some modules never reach an image even though the application works on a full JDK, and how to fix that.
Why jlink can miss modules
A requires directive is a static dependency. jlink follows it when it computes the set of modules for an image. The set starts at the modules named by --add-modules and grows by every module they require, directly or transitively.
Services work differently. A module declares uses SomeService and looks providers up at run time with java.util.ServiceLoader. A provider module declares provides SomeService with SomeImpl. The consumer never requires the provider, so nothing in a requires chain leads to it. By default jlink leaves the provider out, and the application runs but finds no implementation.
The JVM does bind services itself when it boots. If a provider module is in the image, ServiceLoader finds it. The only question is whether jlink put the module in the image.
JDK modules rely on this too. jdk.zipfs provides a file system provider for ZIP files. jdk.charsets provides extra character sets. jdk.localedata provides locale data beyond English. Code that uses these features can work on a full JDK and break in a trimmed image.
A small service example
The example has three modules. com.example.greeter.api defines the service interface. com.example.greeter.english provides an implementation. com.example.greeter.app consumes the service and does not require the provider.
Create this layout:
src/com.example.greeter.api/module-info.java
src/com.example.greeter.api/com/example/greeter/api/Greeter.java
src/com.example.greeter.english/module-info.java
src/com.example.greeter.english/com/example/greeter/english/EnglishGreeter.java
src/com.example.greeter.app/module-info.java
src/com.example.greeter.app/com/example/greeter/app/Main.javaThe API module:
module com.example.greeter.api {
exports com.example.greeter.api;
}package com.example.greeter.api;
public interface Greeter {
String greet(String name);
}The provider module:
module com.example.greeter.english {
requires com.example.greeter.api;
provides com.example.greeter.api.Greeter
with com.example.greeter.english.EnglishGreeter;
}package com.example.greeter.english;
import com.example.greeter.api.Greeter;
public class EnglishGreeter implements Greeter {
@Override
public String greet(String name) {
return "Hello, " + name;
}
}A provider class needs a public no-argument constructor. The compiler accepts the default constructor of a public class.
The consumer module:
module com.example.greeter.app {
requires com.example.greeter.api;
uses com.example.greeter.api.Greeter;
}package com.example.greeter.app;
import com.example.greeter.api.Greeter;
import java.util.ServiceLoader;
public class Main {
public static void main(String[] args) {
var loader = ServiceLoader.load(Greeter.class);
boolean found = false;
for (Greeter greeter : loader) {
System.out.println(greeter.greet("jlink"));
found = true;
}
if (!found) {
System.out.println("No greeter found");
}
}
}Compile all three modules and package each as a modular JAR:
mkdir -p out mods
javac -d out --module-source-path src $(find src -name "*.java")
jar --create --file mods/greeter-api.jar \
-C out/com.example.greeter.api .
jar --create --file mods/greeter-english.jar \
-C out/com.example.greeter.english .
jar --create --file mods/greeter-app.jar \
--main-class com.example.greeter.app.Main \
-C out/com.example.greeter.app .The default behavior
Build an image from the app module alone:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.greeter.app \
--launcher greet=com.example.greeter.app/com.example.greeter.app.Main \
--output build/plain
build/plain/bin/java --list-modules
build/plain/bin/greetThe module list contains com.example.greeter.api, com.example.greeter.app, and java.base. It does not contain com.example.greeter.english. The launcher prints No greeter found.
Option 1: --bind-services
--bind-services tells jlink to add every module on the module path that provides a service used by a module already in the image.
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.greeter.app \
--bind-services \
--launcher greet=com.example.greeter.app/com.example.greeter.app.Main \
--output build/bound
build/bound/bin/java --list-modules
build/bound/bin/greetThe list now includes com.example.greeter.english, and the launcher prints Hello, jlink.
The binding is transitive. A provider can itself use services, and jlink then binds providers for those services too. Because the module path contains $JAVA_HOME/jmods, the search covers every JDK module as well. Many JDK modules provide services used by java.base and other modules. On a real application, --bind-services can pull in dozens of extra modules.
Option 2: name the providers
You can skip service binding and list the provider modules yourself:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.greeter.app,com.example.greeter.english \
--launcher greet=com.example.greeter.app/com.example.greeter.app.Main \
--output build/explicitThis image has the same module set as the bound image, and nothing else gets added. The cost is that you must know which providers you need. For your own modules that is easy. For JDK modules it takes some research.
Option 3: ask jlink for suggestions
--suggest-providers lists modules on the module path that provide a given service. jlink prints the list and builds no image:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--suggest-providers com.example.greeter.api.GreeterUse it to find candidate providers, then add the ones you want with --add-modules. It also works with JDK service types, such as java.nio.file.spi.FileSystemProvider, to find which jdk.* module supplies an implementation.
Choosing an approach
| Approach | What it adds | Best for |
|---|---|---|
| Default | Only modules reachable through `requires` | Applications with no run-time service lookups |
| `--bind-services` | Every provider of every used service | Quick experiments, or applications with many plugins |
| Explicit `--add-modules` | Exactly the modules you name | Production builds that need a predictable image |
| `--suggest-providers` | Nothing (prints candidates) | Discovering what to add |
For production, prefer the explicit list. It makes the image contents a reviewed decision instead of a side effect of whatever is on the module path.
Common errors
- Missing
uses. A consumer module that callsServiceLoader.load(Greeter.class)withoutuses com.example.greeter.api.Greeter;fails at run time with aServiceConfigurationError. The error occurs in a named module on a full JDK as well as in an image. - Provider not on the module path.
--bind-servicesonly finds providers jlink can see. A provider JAR missing from--module-pathis silently skipped. - Classpath-style registration. A
META-INF/servicesfile inside a modular JAR does not make a module a provider. For a named module, onlyprovides ... withcounts. - Trusting
jdepsfor providers.jdepsanalyzes static references. Code that obtains a charset, locale, or file system through a service can run fine with the modulesjdepsreports and still fail in the image. Test behaviors that depend on locale, charset, or ZIP file systems directly. - Output that differs between machines.
--bind-servicesdepends on what is on the module path. Two machines with different JDK builds can produce images with different module sets, so check--list-modulesin CI.
Exercise
The image built from only --add-modules com.example.greeter.app prints "No greeter found", although the same modules work when launched with java --module-path mods -m com.example.greeter.app/com.example.greeter.app.Main. Why, and what are two ways to fix the image?
Show answer
In the java run, the module system performs service binding at boot and resolves com.example.greeter.english from the module path, because it provides a service the app uses. The jlink image has no module path, only the modules jlink put in it. jlink followed requires edges only, so the provider was left out. Fix it with --bind-services, or by adding the provider explicitly: --add-modules com.example.greeter.app,com.example.greeter.english. The explicit list is more predictable.
Exercise
You build one image with --bind-services and one with explicit --add-modules. How do you check whether --bind-services added modules you do not need?
Show answer
Run build/bound/bin/java --list-modules and build/explicit/bin/java --list-modules, then compare the two lists, for example with diff. Any module that appears only in the bound image was added by service binding. Each one should either be needed by a service your application uses or be left out by switching to the explicit list.