Linked Questions

Popular Questions

I have this project developed with IntelliJ IDEA. This project is a demo for Java 9 service modules. I followed the book Modular Programming in Java 9's Introducing Services chapter.

enter image description here

Source code

  1. service module

module name: packt.sortutil

class full name: packt.util.SortUtil

public interface SortUtil {
    <T extends Comparable> List<T> sortList(List<T> list);

2. provider module 1

module name: packt.sort.bubblesort

class full name: packt.util.impl.bubblesort.BubbleSortUtilImpl


package packt.util.impl.bubblesort;
public class BubbleSortUtilImpl implements SortUtil 

module packt.sort.bubblesort {
    requires packt.sortutil;
    provides packt.util.SortUtil with packt.util.impl.bubblesort.BubbleSortUtilImpl;

Provider module 2 packt.sort.javasort is the same with the only difference of a different implementation.

  1. consumer module

module packt.addressbook {
    requires packt.sortutil;
    uses packt.util.SortUtil; 


Iterable<SortUtil> sortUtils = ServiceLoader.load(SortUtil.class);
for (SortUtil sortUtil : sortUtils){
    System.out.println("Found an instance of SortUtil");

The problem I found when running the Main class in consumer module is, provider1(packt.sort.bubblesort) and provider2(packt.sort.javasort) modules are not compiled. The reason is obvious: since instead of the service module reading the provider modules, it is the other way around. The compiler did not see the provider modules as no modules read them, so they are omitted in compilation.

I have 2 questions:

  1. Is there a way to let IntelliJ IDEA automatically discover and compile provider modules for a service module(packt.sortutil in this case)?

  2. Is there at least a way to manually add these 2 provider modules during compilation?

I tried add --add-modules packt.sort.bubblesort inside Run > Edit Configurations > VM Options as suggested by @nullpointer, but the arguments were added to java command. Since the module were not compiled, it shows Module packt.sort.bubblesort not found:

D:\programs\java\jdk-11.0.2\bin\java.exe --add-modules packt.sort.bubblesort "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=49375:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -p "D:\ebooks\Modular Programming in Java 9\practice-code\p205_Implementing_sorting_services\out\production\packt.addressbook;D:\ebooks\Modular Programming in Java 9\practice-code\p205_Implementing_sorting_services\out\production\packt.sortutil;D:\ebooks\Modular Programming in Java 9\practice-code\p205_Implementing_sorting_services\out\production\" -m packt.addressbook/packt.addressbook.Main
Error occurred during initialization of boot layer
java.lang.module.FindException: Module packt.sort.bubblesort not found

Then I manually set the consumer module(packt.addressbook) to depend on the provider modules(packt.sort.bubblesort and packt.sort.javasort, highlighted in blue) inside IntelliJ IDEA(only in IDE, in terms of JPMS consumer module doesn't depend on the provider modules). It works all right now even though I am not sure it's the best solution.

enter image description here

Related Questions