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.
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 of tester.client
module.
Solution provided by @samabcde
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.