且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JDK 11和JavaFx中创建JAR文件

更新时间:2022-03-10 17:46:03

首先,提一个建议:完全不建议在命令行上创建胖子.这是应该避免的手动过程.如果您仍然需要胖子,则应尝试使用Maven或Gradle以及现有插件来完成此任务.相反,您应该尝试创建一个模块化项目,并使用 jlink 创建一个自定义映像,您可以为给定平台分发该映像.

First of all, a word of advice: Creating fat jars on command line is not advised at all. It is a manual process that should be avoided. You should try Maven or Gradle and the existing plugins for this task, in case you still need a fat jar. You should try to create a modular project, instead, and create a custom image with jlink that you can distribute for a given platform.

第二,如果您仍然想手动做一个胖子罐,那么您应该了解上述步骤,以便能够根据需要对其进行修改.基本上,胖子罐的主要思想是一个单独的项目,其中包含来自所有可能依赖项的所有* .class文件(和其他资源),而不仅仅是源代码中的依赖项.openjfx.io上的教程仅提到JavaFX依赖项的情况.

Second, if you still want to do a fat jar manually, you should understand the above steps, in order to be able to modify them according to your needs. Basically the main idea of a fat jar is a single project with all the *.class files (and other resources) from all the possible dependencies, not only those from your source code. The tutorial at openjfx.io just mentions the case of JavaFX dependencies.

因此,如果您有更多的依赖项,则想法是以与提取JavaFX jar相同的方式提取其jar的内容.参见 jar命令选项,例如 xf .

So if you have extra dependencies, the idea is to extract the content of their jars, in the same way you extract the JavaFX jars. See jar command options like xf.

这是在步骤3中完成的:

That is done in the step 3:

cd out & jar xf "%PATH_TO_FX%\javafx.base.jar" & \
    jar xf "%PATH_TO_FX%\javafx.graphics.jar" & \
    jar xf "%PATH_TO_FX%\javafx.controls.jar" & \
    cd .. 

您可以使用现有的jar修改该步骤:

You can modify that step with your existing jars:

cd out & jar xf "%PATH_TO_FX%\javafx.base.jar" & \
    jar xf "%PATH_TO_FX%\javafx.graphics.jar" & \
    jar xf "%PATH_TO_FX%\javafx.controls.jar" & \
    jar xf "C:\sqlite-jdbc-3.6.20.1.jar" & \
    jar xf "C:\pdfbox-app-2.0.10.jar" & \
    cd .. 

然后,您可以按照相同的方式继续执行其余步骤.

Then you can proceed with the rest of the steps in the same way.

请注意,尽管这可能可行,但是那些第三方jar也可能具有其他依赖关系(请参阅其pom.xml).在这种情况下,您将需要手动下载它们,并以相同的方式将其添加到胖子罐中.如果您使用Maven或Gradle,它们会自动为您完成.

Note that while this might work, those third party jars might have other dependencies as well (see their pom.xml for that). If that's the case, you will need to download them manually, and add them to the fat jar in the same way. Should you use Maven or Gradle, they will do it automatically for you.

如前所述,完全不建议这样做.

As mentioned before, this is not advised at all.