1

I would like to execute a jar with JavaFX on any platform, whatever JavaFX is installed or not on the host, using maven.

This is working as long as I run my class with

mvn exec:java

However, I can't produce a jar with dependencies working. At runtime, I got the following (it compiles)

 Error: JavaFX runtime components are missing, and are required to run this application.

In my pom, I have the following:

<dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>11</version>
</dependency>
...
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>Gui</mainClass>
        </configuration>
</plugin>
<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>Gui</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> 
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
</plugin>

So I don't get why it works with exec:java (so meaning that the dependencies are OK) but not via the jar...

Olf
  • 374
  • 1
  • 20
  • 1
    Possible duplicate of [this](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing) and [this](https://stackoverflow.com/questions/52741129/different-behaviour-between-maven-eclipse-to-launch-a-javafx-11-app) – José Pereda Jan 18 '19 at 17:38

1 Answers1

3

As pointed in comment by José Pereda, this answer solved it. To be short, I solved the problem by creating a class extending JavaFX's Application but not making this class as the main class in the jar. Instead, I call the main method of the extended Application into my real main class.

Example:

public class RealMain { //the class in the manifest
    public static void main(String[] args) {
        Gui.main(args);
    }
}

public class Gui extends Application{
    public static void main(String[] args) {
        launch();
    }
}
Olf
  • 374
  • 1
  • 20