在JDK 11和JavaFx中创建JAR文件 [英] JAR File Creation in JDK 11 and JavaFx

查看:37
本文介绍了在JDK 11和JavaFx中创建JAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经了解到JDK-10,JavaFX曾经是JDK的一部分,但随着JDK-11的发布,JavaFX将被单独包含.

As we already know up to JDK-10 JavaFX used to be a part of JDK but with the release of JDK-11, JavaFX is to be included separately .

为此,我们需要为JavaFX提供 VM参数,如下所示:-

For doing that we need to provide VM argument for JavaFX like this:-

--module-path "C:\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls,javafx.fxml

到目前为止,一切正常,但是当我们最终创建要分发的JAR文件时,将出现如下消息:

Up to this point every thing is ok but when we are finally creating a JAR file for distribution then a message is appearing like this:

VM参数不会成为可运行的JAR的一部分.启动JAR时,可以在命令行上传递参数

VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR

因此,现在JAR文件无法打开应用程序.

Therefore now JAR file is not able to open the application.

现在,请通过一些建议的方式为我提供帮助,以便用户可以通过单击JAR图标(比以前更早)来打开应用程序.

Now please help me by suggesting some way out so that user can open application just by clicking on JAR icon as it used to be earlier.

编辑

应用由以下人员提供的解决方案后 openjfx.io (非模块化项目),我能够生成一个独立的Jar用JavaFX归档.

After applying the solution provided by openjfx.io (section non-modular projects), I am able to generate a Standalone Jar File with JavaFX.

现在,我想添加一些本地依赖项,例如 pdfbox Sqlite :

Now I want to add some local dependencies like pdfbox and Sqlite:

sqlite =C:\sqlite-jdbc-3.6.20.1.jar 
pdfbox=C:\pdfbox-app-2.0.10.jar

我正在执行以下步骤:

第1步

cd eclipse-workspace2018\test101

第2步

set PATH_TO_FX="C:\javafx-sdk-11.0.1\lib"

第3步

dir /s /b src\*.java > sources.txt & \
    javac --module-path %PATH_TO_FX% --add-modules=javafx.controls \
    -d out @sources.txt & del sources.txt

第4步

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 .. 
copy "%PATH_TO_FX%\..\bin\prism*.dll" out & \
    copy "%PATH_TO_FX%\..\bin\javafx*.dll" out & \
    copy "%PATH_TO_FX%\..\bin\glass.dll" out & \
    copy "%PATH_TO_FX%\..\bin\decora_sse.dll" out 
del out\META-INF\MANIFEST.MF & del out\module-info.class 
mkdir libs 
jar --create --file=libs/index101.jar \
    --main-class=test101.Launcher -C out . 
java -jar libs\index101.jar

请修改我的步骤,以添加上述两个依赖项pdfBox&sqlite.

Kindly modify my steps for adding above mentioned two dependencies pdfBox & sqlite.

推荐答案

首先,提一个建议:完全不建议在命令行上创建胖子.这是应该避免的手动过程.如果您仍然需要胖子,则应尝试使用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.

这篇关于在JDK 11和JavaFx中创建JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆