Build executable JAR with JavaFX11 from maven

岁酱吖の 提交于 2019-12-02 10:37:44

I struggled through the process of creating an executable jar as well, but this workaround is what worked for me, and I hope it works for you as well:

First of all, instead of using the jar plugin, I used the shade plugin in pom.xml, which creates a "fat jar" or "uber jar" that contains your classes and all of the dependencies within the jar. This way, your jar will be included with all the necessary javafx packages and classes. That is, if you include these in the <dependencies> section:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>11</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>11</version>
</dependency>

... or whatever else of the javafx libraries you need in order for your application to run.

Simply doing this does not work, however. I'm assuming that your main class Entry extends Application?

I'm guessing the jar needs to know the actual Main class that does not extend Application, so I just created another Main class called SuperMain (it was only a temporary name) that calls my original main class, which is Main:

// package <your.package.name.here>

public class SuperMain {
    public static void main(String[] args) {
        Main.main(args);
    }
}

whereas in your case it's Entry.

So in my pom.xml, I have a plugin called shade that looks like this:

<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>my.package.name.SuperMain</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

and there should be a jar that's "shaded" after you execute mvn package.

Thanks to the answer to this post: JavaFX 11 : Create a jar file with Gradle

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!