I am learning Java 9 Module by reading the book "Modular Programming in Java 9". In chapter 7, Introducing services, at page 201, I followed the steps by the book to create a simple project using service. I will post the code below or you can visit the project on github.
- service
module name: packt.sortutil
class full name: packt.util.SortUtil
public interface SortUtil {
<T extends Comparable> List<T> sortList(List<T> list);
}
2. providers class full name: packt.util.impl.bubblesort.BubbleSortUtilImpl: code:
package packt.util.impl.bubblesort;
public class BubbleSortUtilImpl implements SortUtil
...
module-info.java
module packt.sort.bubblesort {
requires packt.sortutil;
provides packt.util.SortUtil with packt.util.impl.bubblesort.BubbleSortUtilImpl;
}
There is also packt.util.impl.javasort.JavaSortUtilImpl with the only difference of a different implementation.
- consumer
module-info.java:
module packt.addressbook {
requires packt.sortutil;
uses packt.util.SortUtil;
}
code:
Iterable<SortUtil> sortUtils = ServiceLoader.load(SortUtil.class);
for (SortUtil sortUtil : sortUtils){
System.out.println("Found an instance of SortUtil");
sortUtil.sortList(contacts);
}
When I run consumer, it should be:

Note the highlighted area, where 2 implementations are found. Also, the names are sorted by the last name.
But for my program, it is:
2月 03, 2019 12:12:55 上午 packt.addressbook.Main main
INFO: Address book viewer application: Started
2月 03, 2019 12:12:56 上午 packt.addressbook.Main main
INFO: Address book viewer application: Completed
[Edsger Dijkstra, Raid Black]
Notice there are no "Found an instance of SortUtil" and the names are not sorted. So it appears as if there are no implementations registered with the service.
Where did it go wrong? Thanks.