java 部署 JavaFX 应用程序、创建 JAR 和自包含应用程序以及本机安装程序的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30145772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to deploy JavaFX application, create JAR and self-contained applications and native installers
提问by Jean-Baptiste-B
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.
我正在使用 IntelliJ IDEA,并且已准备好部署 JavaFX 应用程序。问题是当我生成 JAR 文件时,它不会运行,当我在命令行中运行它时,我得到一个异常 FXMLLoadException,尽管该项目在我的 IDE 中运行良好。
Ant tasks end with errors, after 15 minutes of building, I really don't understand what's the problem exactly.
Ant任务以错误结束,构建15分钟后,我真的不明白到底是什么问题。
So my question what are the right steps to deploy a JavaFX application the right way, any tutorial or guide will be welcome.
所以我的问题是以正确的方式部署 JavaFX 应用程序的正确步骤是什么,欢迎任何教程或指南。
回答by ItachiUchiha
A Java application can be packaged in various ways. Please go through Java Packaging Overviewto find everything about it. One of the packaging is a self-contained Java application
.
Java 应用程序可以通过多种方式打包。请浏览Java Packaging Overview以找到有关它的所有信息。其中一个包装是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
- 使用
javapackager
JDK 附带的工具 - JavaFX Ant 任务
- 用于 Maven 项目的 JavaFX Maven 插件
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
- 应用程序包
- 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
Java打包工具
JavaPackager Toolis 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.
JavaPackager Tool是最基本的工具,可帮助您编译、打包、签名和部署 Java(FX) 应用程序,而无需编写任何其他脚本。该javapackager.jar
文件位于JDK安装的bin目录中。
The list of commands, that can be used with it is available here.
可与它一起使用的命令列表可在此处获得。
JavaFX Ant Tasks
JavaFX Ant 任务
JavaFX Ant Taskshelps you package your application by just creating a build.xml
file for your project.
JavaFX Ant Tasks帮助您打包应用程序,只需build.xml
为您的项目创建一个文件。
A set of examples on how to use ant scripts for your project can be found here.
可以在此处找到一组有关如何为您的项目使用 ant 脚本的示例。
The list of commands that can be used for with it is available here.
可用于它的命令列表可在此处获得。
JavaFX Maven Plugin
JavaFX Maven 插件
JavaFX Maven Pluginleverages 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.
JavaFX Maven Plugin利用打包java应用到maven平台的使用。您可以通过向项目添加插件来使用它来打包基于 Maven 的 Java 应用程序。
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.
这个插件恕我直言,如果easiest
用的出三个。这是一个写得非常好,易于理解的工具,并有大量的文档。
JavaFX Gradle Plugin
JavaFX Gradle 插件
JavaFX Gradle Pluginis also from the author of the maven plugin. It has all the features that the maven plugin has, but for Gradle :)
JavaFX Gradle Plugin也是来自 maven 插件的作者。它具有 maven 插件的所有功能,但适用于 Gradle :)
Further Reading :
进一步阅读:
回答by Roland
回答by Mark Kowalski
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.
如果您使用 Maven,另一种非常聪明的方法是使用maven-shade-plugin
( link)。它构建了一个包含所有依赖项的大胖 jar,您可以在其中直接执行而无需任何其他操作。您可以将其与javafx-maven-plugin
. 我也尝试了不同的方法并玩了很长时间,这个解决方案是唯一真正有效的解决方案。此外,它很容易设置。
Here is what you have to add to your pom.xml:
这是您必须添加到 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
.
更改您的包名mainClass
以用于 shade 和 javaFx 插件,您就完成了。现在,您可以像往常一样使用mvn package
.