Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundling JavaFX Library with Maven in executable JAR (for Java 11+)

I'm using Java 8 with JavaFX. When I package my executable JAR with maven, the executable JAR works fine using Java 8. However, if I run the JAR with e.g. Java 13, I get the following error:

Error: Could not find or load main class ApplicationLauncherClient
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

This is presumably caused by JavaFX not being bundled with the JRE/JDK anymore since Java 11.

My pom.xml build configuration looks as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>ApplicationLauncherClient</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

My goal is to instruct maven to build a JAR which can run on any Java version greater than or equal to 8 by including JavaFX in the executable JAR.

I did not succeed with following the instructions in this answer (by e.g. adding the JavaFX plugin to my pom.xml). Including OpenJFX as dependency also did not work, I still receive the same error.

like image 692
BullyWiiPlaza Avatar asked Dec 03 '25 03:12

BullyWiiPlaza


1 Answers

I use the javafx-maven-plugin and the maven-shade-plugin like so:

    <build>
        <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
            <release>11</release>
            <target>8</target>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.openjfx</groupId>
          <artifactId>javafx-maven-plugin</artifactId>
          <version>0.0.3</version>
          <configuration>
            <mainClass>Your_main_class</mainClass>
          </configuration>
      </plugin>
                <plugin>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                    <execution>
                        <phase>package</phase>
                    <goals>
                    <goal>shade</goal>
                    </goals>
                    <configuration>
                    <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>Your_main_class_starter</mainClass>
                    </transformer>
                    </transformers>
                    </configuration>
                    </execution>
                    </executions>
                </plugin>              
    </plugins>
</build>

Your_main_class is something like this:

public class App extends Application {

private static Scene scene;

@Override
public void start(Stage stage) throws IOException {
    Parent mainForm = FXMLLoader.load(getClass().getResource("mainForm.fxml"));
    scene = new Scene(mainForm , 425, 275);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch();
}
}

And Your_main_class_starter looks like this:

public class LaunchApp {
public static void main(String[] args) {
    App.main(args);
}

So Your_main_class_starter does nothing else but calling your main class.

like image 174
Michael Avatar answered Dec 04 '25 17:12

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!