title: JavaFX module programmatically loading
body: <p>Good day</p>
  <p>I use, Win11 Pro 64 bit, ENG version, I use Oracle Java 11 or 17(problem on both), Eclipse IDE 2022_06. I would like load JavaFX modules programmatically in my throws project. In any folder on my harddrive i have JavaFX11 libraries. In the same folder i have simple JavaFX11 program(only show window with button, nothing more). This JavaFX program has name &quot;ProgramJavaFX11.jar&quot; and i can start it with command :
  <code>java --module-path &quot;D:\Java\X_Package\JavaFX11\lib&quot; --add-modules=javafx.controls,javafx.fxml -cp ProgramJavaFX11.jar javafx.ProgramJavaFX11</code> and its work of course.</p>
  <p>I was use standard <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ModuleLayer.html" rel="nofollow noreferrer">JavaAPI</a> with added example, but its not work. If somebody know the answer can help me?</p>
  <pre><code>package spust;
  import java.lang.module.*;
  import java.nio.file.*;
  import java.util.*;
  public class LoadFXModule {
  public static void main(String[] args) {
      System.out.println(&quot;Start Program loadJAVAFXlibs&quot;);
      try {
          Path [] paths = {Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.base.jar&quot;),Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.controls.jar&quot;),
                  Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.fxml.jar&quot;),Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.graphics.jar&quot;),
                  Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.media.jar&quot;),Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.swing.jar&quot;),
                  Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx.web.jar&quot;),Paths.get(&quot;D:\\Java\\X_Package\\JavaFX11\\lib\\javafx-swt.jar&quot;),
                  Paths.get(&quot;D:\\JavaProjekty\\Java.Zaklady.JarVJarFXlib\\source\\ProgramJavaFX11.jar&quot;)}; 
          ModuleFinder finder = ModuleFinder.of(paths);
          ModuleLayer parent = ModuleLayer.boot();
          Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), java.util.Set.of(&quot;javafx.controls&quot;,&quot;javafx.fxml&quot;));
          ClassLoader systemLoader = ClassLoader.getSystemClassLoader();          //  Name loader :app
          //ClassLoader systemLoader = ClassLoader.getPlatformClassLoader();      //  Name loader :platform
          System.out.println(&quot;Name loader :&quot;+systemLoader.getName());
          ModuleLayer layer = parent.defineModulesWithOneLoader(cf, systemLoader);
          
          // HERE ITS CRASH
          Class&lt;?&gt; javaFXClass = layer.findLoader(&quot;javafx.base&quot;).loadClass(&quot;javafx.ProgramJavaFX11&quot;);
          /*
          Method metodaMain = javaFXClass.getMethod(&quot;main&quot;,String[].class);
          if(metodaMain == null) System.out.println(&quot;methodMain is null&quot;);    
          String [] pole1String = {&quot;&quot;,&quot;&quot;};
          Object argsArray [] = { pole1String };
          metodaMain.invoke(null,argsArray);
          */
      } catch(Exception e) {
          e.printStackTrace();    }       
      System.out.println(&quot;End Program&quot;);
  }
  </code></pre>
  <p>Program crash with this message. It crash in IDE and runnable.jar too. It is look the classloader(or module loader) do not have access to &quot;ProgramJavaFX11.jar&quot; but i set to Path on beginning of code.</p>
  <pre><code>java.lang.ClassNotFoundException: javafx.ProgramJavaFX11
  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
  at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
  at java.base/jdk.internal.loader.Loader.loadClass(Loader.java:544)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
  at spust.LoadFXModule.main(LoadFXModule.java:92)
  </code></pre>
  <p>Here is the simple project(without module-info.java, so it is automatic module) runnable jar ProgramJavaFX11.jar (really only window with button) what i want to run. I have change the name of package from &quot;javafx&quot; to &quot;fxpackage&quot;. I create standard Project in EclipseIDE,set Oracle JDK11, set Java Build path -&gt; libraries -&gt; classpath and here i added JavaFX11 libraries :</p>
  <ul>
  <li>javafx.base.jar</li>
  <li>javafx.controls.jar</li>
  <li>javafx.fxml.jar</li>
  <li>javafx.graphics.jar</li>
  <li>javafx.media.jar</li>
  <li>javafx.swing.jar</li>
  <li>javafx.web.jar</li>
  <li>javafx.swt.jar</li>
  </ul>
  <p>Here is the source code:</p>
  <pre><code> package fxpackage;
      import javafx.application.*;
      import javafx.event.*;
      import javafx.geometry.Pos;
      import javafx.scene.*;
      import javafx.scene.control.*;
      import javafx.scene.control.Alert.AlertType;
      import javafx.scene.layout.*;
      import javafx.stage.Stage;
  public class ProgramJavaFX11 extends Application {
      private Scene scene;
      public static void main(String[] args) {
          Application.launch(args);
      }
      public void start(Stage stage) throws Exception {
          Button tlac = new Button(&quot;Press me&quot;);
          tlac.setPrefSize(200,100);      
          tlac.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              public void handle(ActionEvent ae) {
                  Alert alert = new Alert(AlertType.INFORMATION);
                  alert.setTitle(&quot;Warning&quot;);
                  alert.setContentText(&quot;Good day&quot;);
                  alert.show();
              }
          });
          FlowPane flowpane = new FlowPane(10,10);
          flowpane.setAlignment(Pos.CENTER);
          flowpane.setStyle(&quot;-fx-background-color: rgb(193, 122, 68)&quot;);
          flowpane.getChildren().add(tlac);
          scene = new Scene(flowpane, 500, 250);
          stage.setScene(scene);
          stage.setTitle(&quot;Window with JavaFX11&quot;);
          stage.show();
      }
  }
  </code></pre>
  <p>I exported this project in Eclipse as runnable jar and can run it with on command-line with command :</p>
  <p><code>java --module-path &quot;D:\Java\X_Package\JavaFX11\lib&quot; --add-modules=javafx.controls,javafx.fxml -cp ProgramJavaFX11.jar fxpackage.ProgramJavaFX11</code></p>