What is the best way to deploy JavaFX application, create JAR and self-contained applications and native installers

放肆的年华 提交于 2019-11-26 06:44:19

问题


I\'m using IntelliJ IDEA, and I have my JavaFX application ready for deployment. The problem is that when I generate the JAR file, it won\'t run, when I run it in the command line, I get an Exception, FXMLLoadException, although the project is working perfectly in my IDE.

Ant tasks end with errors, after 15 minutes of building, I really don\'t understand what\'s the problem exactly.

So my question what are the right steps to deploy a JavaFX application the right way, any tutorial or guide will be welcome.


回答1:


A Java application can be packaged in various ways. Please go through Java Packaging Overview to find everything about it. One of the packaging is a self-contained Java application.

There are different ways to create these packages :

  • Use the javapackager tools that comes with your JDK
  • JavaFX Ant Tasks
  • JavaFX Maven Plugin for a maven project

Self-contained application is one the ways how your application can be packaged and is platform specific. The bundle contains :

  • The application package
  • A private copy of the JRE

A list of available bundles can be found here.

Let us check out the various tools available at our disposal and how to use them:

JavaPackager Tool

JavaPackager Tool is the most basic tool and helps you to compile, package, sign, and deploy your Java(FX) applications, without writing any additional scripts. The javapackager.jar file is located in the bin directory of the JDK installation.

The list of commands, that can be used with it is available here.

JavaFX Ant Tasks

JavaFX Ant Tasks helps you package your application by just creating a build.xml file for your project.

A set of examples on how to use ant scripts for your project can be found here.

The list of commands that can be used for with it is available here.

JavaFX Maven Plugin

JavaFX Maven Plugin leverages the usage of packaging java application to the maven platform. You can use it to package your maven based java application by adding a plugin to the project.

This plugin IMHO, if the easiest to use out of the three. It is a very nicely written, easy to understand tool and has extensive documentation.

JavaFX Gradle Plugin

JavaFX Gradle Plugin is also from the author of the maven plugin. It has all the features that the maven plugin has, but for Gradle :)

Further Reading :

  • Native packaging for JavaFX
  • JavaFX packaging tools update



回答2:


Oracle has an extensive guide about packaging on their website for JavaFX 2 and for JavaFX 8.

If you need help for your specific problem, post the code and the stacktrace of the error you get.




回答3:


If you use Maven, another very smart approach is to use maven-shade-plugin (link). It builds one big fat jar with all dependencies inside which you can execute directly without any further things to do. You can use this in combination with javafx-maven-plugin. I also tried different approaches and played around a long time and this solution was the only one which really works. Additionally, it was easy to set up.

Here is what you have to add to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>your.package.name.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
            <configuration>
                <mainClass>your.package.name.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Change your package name inside mainClass for shade and also javaFx plugin and you are done. Now you can build your Application as always with mvn package.



来源:https://stackoverflow.com/questions/30145772/what-is-the-best-way-to-deploy-javafx-application-create-jar-and-self-contained

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