I can't get the Service Provider Interface to load an implementation from another JAR in the same directory. It only works when I use -Djava.ext.dirs=. on the command line. Should it not work without?
I have the following interface:
package playground;
public interface TestIface {
public String test();
}
which is implemented here:
package playground;
public class TestImpl implements TestIface {
public String test() {
return "TEST";
}
}
Here I try to load the implementation:
package playground;
import java.util.Iterator;
import java.util.ServiceLoader;
public class Lalala {
public static void main(String[] args) {
ServiceLoader<TestIface> loader = ServiceLoader.load(TestIface.class);
Iterator<TestIface> it = loader.iterator();
while (it.hasNext()) {
TestIface a = it.next();
System.out.println(a.test());
}
System.out.println("DONE");
}
}
The interface and the last class are packaged in main.jar, the implementation in impl.jar. main.jar has the Main class set and impl.jar has the META-INF/services/playground.TestIface file which contains "playground.TestImpl". Both JARs are in the same directory. Running
java -jar main.jar
only prints "DONE", the implementation apparently is not found.
If I instead run
java -Djava.ext.dirs=. -jar main.jar
it also prints "TEST" as it should.
What am I doing wrong? Why is the implementation from the other JAR not loaded unless I change the java.ext.dirs setting?