如何使用 JDK 11 打开 JavaFX .jar 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53533486/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:49:57  来源:igfitidea点击:

How to open JavaFX .jar file with JDK 11?

javajavafxjava-11openjfx

提问by DrMorteza

I created a JavaFX project in IntelliJ. I can run project in IntelliJ. I added below code in Configurations):

我在 IntelliJ 中创建了一个 JavaFX 项目。我可以在 IntelliJ 中运行项目。我在配置中添加了以下代码):

--module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml

But the output .jar file of project (made with Artifects) doesn't run. I tested these commands, but didn't get any chance:

但是项目的输出 .jar 文件(用 Artifects 制作)没有运行。我测试了这些命令,但没有任何机会:

java  --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml -jar Timer.jar
java  --module-path %PATH_TO_FX% --add-modules javafx.controls  Timer.jar

Last error log of command line:

命令行的最后错误日志:

Error: Could not find or load main class Files\Java\javafx-sdk-11.0.1\lib
Caused by: java.lang.ClassNotFoundException: Files\Java\javafx-sdk-11.0.1\lib

p.s: I could run .jar file of this project when build on JDK-10

ps:在JDK-10上构建时我可以运行这个项目的.jar文件

EDIT:

编辑

I downloaded JavaFX and added it's lib folder to System Environments. for adding JavaFX to project I did this process: Project Structure > Libraries > add > Java > JavaFxPath/lib

我下载了 JavaFX 并将它的 lib 文件夹添加到系统环境中。将 JavaFX 添加到项目我做了这个过程:项目结构 > 库 > 添加 > Java > JavaFxPath/lib

Then I created Artifect for output jar file in this process: Project Structure > Artifects > Add > JAR > From Modules with dependencies > main Class : main.Main.

然后我在这个过程中为输出 jar 文件创建了 Artifect:项目结构 > 工件 > 添加 > JAR > 来自具有依赖项的模块 > 主类:main.Main。

采纳答案by José Pereda

Providing you have a simple (non-modular) JavaFX 11 project (without Maven/Gradle build tools), and you are using IntelliJ, like the HelloFX sample from here, this is how you can create a jar from IntelliJ that can be run from the console

如果您有一个简单的(非模块化)JavaFX 11 项目(没有 Maven/Gradle 构建工具),并且您正在使用 IntelliJ,就像这里的 HelloFX 示例一样,这就是您如何从 IntelliJ 创建一个可以运行的 jar 的方法控制台

A full tutorial on how to run the project can be found here, and instructions on how to create a jar are here(see section Non-modular project), but these doesn't cover Artifactsfrom IntelliJ.

有关如何运行该项目的完全手册,可以发现在这里,并就如何创建一个罐子说明这里(见非模块化的项目),但这些并不包括Artifacts来自的IntelliJ。

Check that the HelloFX project runs from IntelliJ with these VM options:

使用以下 VM 选项检查 HelloFX 项目是否从 IntelliJ 运行:

--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml

where PATH_TO_FXhas been set in File -> Settings -> Appearance & Behavior -> Path Variables, pointing to the JavaFX SDKlib.

wherePATH_TO_FX已设置File -> Settings -> Appearance & Behavior -> Path Variables,指向 JavaFX SDK库。

Semifat Jar

脂肪罐

We can create a Jar that only contains the classes from the project, and third party dependencies, but not JavaFX ones.

我们可以创建一个仅包含项目类和第三方依赖项的 Jar,但不包含 JavaFX 的类。

Go to File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies, add your main class, accept.

转到File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies,添加您的主类,接受。

Then remove the JavaFX jarsfrom the list, and accept.

然后从列表中删除 JavaFX jar,并接受。

SemiJar

SemiJar

Build the project, it will create a quite small jar (3 KB in this case).

构建项目,它将创建一个非常小的 jar(在本例中为 3 KB)。

Now you should be able to run it like:

现在你应该能够像这样运行它:

java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml -jar out\artifacts\HelloFX_jar\HelloFX.jar

(make sure that %PATH_TO_FX%points to a valid folder and use quotes if it contains spaces.

(确保%PATH_TO_FX%指向一个有效的文件夹,如果它包含空格,请使用引号。

You can distribute this jar, and run it in other platforms, providing those also have the JavaFX SDK.

您可以分发这个 jar,并在其他平台上运行它,前提是这些平台也有 JavaFX SDK。

Fat Jar

脂肪罐

If you want a full fat jar that includes JavaFX dependencies, you can still use Artifacts.

如果您想要一个包含 JavaFX 依赖项的完整 jar,您仍然可以使用 Artifacts。

Go to File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies, add your main class, accept.

转到File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies,添加您的主类,接受。

Then keep the JavaFX jarsfrom the list, and accept. Build the project.

然后从列表中保留 JavaFX jars,并接受。构建项目。

In theory, you should be able to run it like:

理论上,你应该能够像这样运行它:

java -jar out\artifacts\HelloFX_jar\HelloFX.jar

But this won't work.

但这行不通。

Reason 1: You need a launcher class, as explained here.

原因1:您需要一个发射器类,如解释在这里

So create a launcher class:

所以创建一个启动器类:

public class Launcher {

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

Reason 2: If you only add your SDK jars to the fat jar, you will be missing the native libraries, as explained here.

原因2:如果只添加SDK罐子脂肪罐子,你会错过本机库,为解释在这里

So edit the artifact, select the Launcher class as main class, and add the native libraries (Directory Content -> path-to/JavaFX SDK/binon Windows):

因此编辑工件,选择 Launcher 类作为主类,并添加本机库(Directory Content -> path-to/JavaFX SDK/bin在 Windows 上):

Fat Jar

Fat Jar

Now build the project (now the jar is about 33 MB, and contains unnecessary native libraries) and run:

现在构建项目(现在 jar 大约 33 MB,并且包含不必要的本机库)并运行:

java -jar out\artifacts\HelloFX_jar\HelloFX.jar

You can distribute this jar, but only to Windows platforms.

您可以分发这个 jar,但只能分发到 Windows 平台。

You can create similar jars for other platforms, if you download their JavaFX SDKs, and you can also build cross-platform jars if you add them all together, as explained in the linked answers above.

您可以为其他平台创建类似的 jar,如果您下载他们的 JavaFX SDK,您也可以构建跨平台 jar,如果您将它们全部添加在一起,如上面链接的答案中所述。

Anyway, you should consider using jlinkinstead.

无论如何,你应该考虑使用jlink替代

Note

笔记

About this error:

关于这个错误:

Caused by: java.lang.ClassNotFoundException: Files\Java\javafx-sdk-11.0.1\lib

引起:java.lang.ClassNotFoundException: Files\Java\javafx-sdk-11.0.1\lib

it looks like the library path was set without quotes and it is missing the first part of the path C:\Program Files\.... Just make sure you use quotes:

看起来库路径设置没有引号,并且缺少路径的第一部分C:\Program Files\...。只要确保你使用引号:

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

回答by Sai Haridass

I had the similar issue exporting/generating an Jar using JavaFX and IntelliJ Non-modular with Gradle(https://openjfx.io/openjfx-docs/)

我在使用JavaFX 和 IntelliJ Non-modular with Gradle( https://openjfx.io/openjfx-docs/)导出/生成 Jar 时遇到了类似的问题

The jar I was generating using Gradle jar command does not run and throws error saying it can not find my Main Class. When I opened my jar I was able to locate my main class. So I realized the error has to do with Jar Packaging.

我使用 Gradle jar 命令生成的 jar 没有运行并抛出错误,说它找不到我的主类。当我打开罐子时,我能够找到我的主要课程。所以我意识到这个错误与 Jar 包装有关。

enter image description here

enter image description here

I fixed the problem by adding JavaFX SDK to my Java SDk in IntelliJ as shown below.

我通过将 JavaFX SDK 添加到 IntelliJ 中的 Java SDk 解决了这个问题,如下所示。

enter image description here

enter image description here

After this I use the regular Gradle build Jar command to generate my Jar file (as shown below) and it runs Normally.

在此之后,我使用常规的 Gradle build Jar 命令生成我的 Jar 文件(如下所示)并且它正常运行。

enter image description here

enter image description here