title: JPMS: How to let IntelliJ IDEA automatically discover and compile provider modules for a service module?
body: <p>I have this <a href="https://github.com/peckwood/p205_Implementing_sorting_services" rel="nofollow noreferrer">project</a> 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.</p>
  
  <p><a href="https://i.stack.imgur.com/LAuXP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LAuXP.png" alt="enter image description here"></a></p>
  
  <p><strong>Source code</strong></p>
  
  <ol>
  <li>service module</li>
  </ol>
  
  <p>module name: <code>packt.sortutil</code></p>
  
  <p>class full name: <code>packt.util.SortUtil</code></p>
  
  <pre><code>public interface SortUtil {
      &lt;T extends Comparable&gt; List&lt;T&gt; sortList(List&lt;T&gt; list);
  }
  </code></pre>
  
  <p>2. provider module 1</p>
  
  <p>module name: <code>packt.sort.bubblesort</code></p>
  
  <p>class full name: <code>packt.util.impl.bubblesort.BubbleSortUtilImpl</code></p>
  
  <p>class:</p>
  
  <pre><code>package packt.util.impl.bubblesort;
  public class BubbleSortUtilImpl implements SortUtil 
  ...
  </code></pre>
  
  <p>module-info.java:</p>
  
  <pre><code>module packt.sort.bubblesort {
      requires packt.sortutil;
      provides packt.util.SortUtil with packt.util.impl.bubblesort.BubbleSortUtilImpl;
  }
  </code></pre>
  
  <p>Provider module 2 <code>packt.sort.javasort</code> is the same with the only difference of a different implementation.</p>
  
  <ol start="3">
  <li>consumer module</li>
  </ol>
  
  <p>module-info.java:</p>
  
  <pre><code>module packt.addressbook {
      requires packt.sortutil;
      uses packt.util.SortUtil; 
  }
  </code></pre>
  
  <p>class:</p>
  
  <pre><code>Iterable&lt;SortUtil&gt; sortUtils = ServiceLoader.load(SortUtil.class);
  for (SortUtil sortUtil : sortUtils){
      System.out.println("Found an instance of SortUtil");
      sortUtil.sortList(contacts);
  }
  </code></pre>
  
  <hr>
  
  <p>The problem I found when running the Main class in consumer module is, provider1(<code>packt.sort.bubblesort</code>) and provider2(<code>packt.sort.javasort</code>) modules are not compiled. The reason is obvious: since instead of the service module reading the provider modules, it is the other way around. <strong>The compiler did not see the provider modules as no modules read them, so they are omitted in compilation.</strong></p>
  
  <p>I have 2 questions:</p>
  
  <ol>
  <li><p>Is there a way to let IntelliJ IDEA automatically discover and compile provider modules for a service module(packt.sortutil in this case)?</p></li>
  <li><p>Is there at least a way to manually add these 2 provider modules during compilation?</p></li>
  </ol>
  
  <p>I tried add <code>--add-modules packt.sort.bubblesort</code> inside <code>Run &gt; Edit Configurations &gt; VM Options</code> as suggested by @nullpointer, but the arguments were added to <code>java</code> command. Since the module were not compiled, it shows <code>Module packt.sort.bubblesort not found</code>:</p>
  
  <pre><code>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\packt.contact" -m packt.addressbook/packt.addressbook.Main
  Error occurred during initialization of boot layer
  java.lang.module.FindException: Module packt.sort.bubblesort not found
  </code></pre>
  
  <p>Then I manually set the consumer module(<code>packt.addressbook</code>) to depend on the provider modules(<code>packt.sort.bubblesort</code> and <code>packt.sort.javasort</code>, 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 <strong>not sure it's the best solution</strong>.</p>
  
  <p><a href="https://i.stack.imgur.com/ZGBlr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZGBlr.png" alt="enter image description here"></a></p>