I found a custom class loader, which loads classes by child-first principle. And it works fine, but I faced with the following issue. When I try to load classes that use SPI I get the exception:
Exception in thread "main" java.util.ServiceConfigurationError: test.spi.SayMyNameProvider: test.spi.ImplProvider not a subtype
I created simple SPI project with modules: spi-api, spi-impl and spi-app.
And it works when I use URLClassLoader, however whenever I use ChildFirstClassLoader I get the exception mentioned above:
public class TestMain {
public static void main(String[] args) throws MalformedURLException {
//!!! comment ChildFirstClassLoader and uncomment URLClassLoader to get the correct behavior
ChildFirstClassLoader classLoader = getCustomClassLoader();
//URLClassLoader classLoader = getUrlClassLoader();
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
try {
List<SayMyNameProvider> providers = Speaker.providers();
for (SayMyNameProvider provider : providers) {
SayMyNameManager sayMyNameManager = provider.create();
sayMyNameManager.sayIt("main");
}
System.out.println("done");
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
private static ChildFirstClassLoader getCustomClassLoader() throws MalformedURLException {
URL[] urls = getUrls();
return new ChildFirstClassLoader(urls);
}
private static URLClassLoader getUrlClassLoader() throws MalformedURLException {
URL[] urls = getUrls();
return new URLClassLoader(urls);
}
private static URL[] getUrls() throws MalformedURLException {
File spiImpl = Paths.get("spi-impl", "target", "spi-impl-1.0.0-SNAPSHOT.jar").toFile();
File spiApi = Paths.get("spi-api", "target", "spi-api-1.0.0-SNAPSHOT.jar").toFile();
URL[] urls = new URL[2];
urls[0] = spiImpl.toURI().toURL();
urls[1] = spiApi.toURI().toURL();
return urls;
}
}
Maybe someone has already faced this problem before and knows how to solve it. I would be grateful for any help or advice.