title: Understanding how the main class affects JPMS
body: <p>I have a very basic JavaFX application that works flawlessly if the Application class is <strong>not</strong> the Main class:</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.stage.Stage;
  
  public class Main {
  
      public static void main(String[] args) {
          Application.launch(App.class, args);
      }
  
  }
  
  public class App extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          FXMLLoader loader = new FXMLLoader(); // works
      }
  
  }
  </code></pre>
  
  <p>However, when I merge the two together (which is the recommended way in most tutorials, including <a href="https://github.com/openjfx/samples/blob/9244c1f2df35f6c1bdddbdc83b48da75af378481/HelloFX/Maven/hellofx/src/main/java/HelloFX.java" rel="nofollow noreferrer">OpenJFX's official documentation</a>), the module system throws an <code>IllegalAccessError</code> (at least on OpenJDK 11.0.2):</p>
  
  <pre><code>public class MainApp extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          FXMLLoader loader = new FXMLLoader(); // throws IllegalAccessError
      }
  
      public static void main(String[] args) {
          launch(MainApp.class, args);
      }
  
  }
  </code></pre>
  
  <p>The exception is:</p>
  
  <blockquote>
    <p>java.lang.IllegalAccessError: class <code>com.sun.javafx.fxml.FXMLLoaderHelper</code> (in unnamed module <code>@0x642c1a1b</code>) cannot access class <code>com.sun.javafx.util.Utils</code> (in module <code>javafx.graphics</code>) because module <code>javafx.graphics</code> does not export <code>com.sun.javafx.util</code> to unnamed module <code>@0x642c1a1b</code></p>
  </blockquote>
  
  <p>The weird thing is, that I <em>did not</em> actively use the module system. I did not add a <code>module-info.java</code> to my project. So I assumed everything should get exported to any unnamed modules? But that is not even the point.</p>
  
  <p>The main question is: Why does the same code behave differently if distributed across two classes? In both cases <code>FXMLLoader</code> uses <code>com.sun.javafx.fxml.FXMLLoaderHelper</code>, which in turn uses <code>com.sun.javafx.util.Utils</code>. So either I should get the exception in both cases or in none. What is the difference?</p>