I'm trying to use ServiceLoader with modules system, the same way as shown in the Deploying service providers as modules header in the documentation here - click
I have the following project:
module tester.client
package tester.client;
import tester.common.Showable;
import java.util.ServiceLoader;
public class Main {
public static void main(String[] args) {
ServiceLoader<Showable> loader = ServiceLoader.load(Showable.class);
loader.findFirst().orElseThrow(); //throws Exception
}
}
module-info.java
import tester.common.Showable;
module tester.client {
requires tester.common;
uses Showable;
}
module tester.common
package tester.common;
public interface Showable {
void show();
}
module-info.java
module tester.common {
exports tester.common;
}
module tester.gui
package tester.gui;
import tester.common.Showable;
public class Window implements Showable {
@Override
public void show() {
}
}
module-info.java
module tester.gui {
requires tester.common;
provides tester.common.Showable with tester.gui.Window;
}
THE PROBLEM:
ServiceLoader doesn't load my implementation.
- tester.client uses Showable
- tester.common exports Showable
- tester.gui provides Showable with Window
It turned out the problem is IDE-specific. As my IDE (Intellij IDEA) has its own additional modules system, I needed to add
tester.gui
module as a dependency oftester.client
module.Solution provided by @samabcde