如何使用 JDK 11 打开 JavaFX .jar 文件? [英] How to open JavaFX .jar file with JDK 11?

查看:68
本文介绍了如何使用 JDK 11 打开 JavaFX .jar 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

命令行的最后一个错误日志:

错误:无法找到或加载主类 FilesJavajavafx-sdk-11.0.1lib引起:java.lang.ClassNotFoundException: FilesJavajavafx-sdk-11.0.1lib

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

编辑:

我下载了 JavaFX 并将其 lib 文件夹添加到系统环境.为了将 JavaFX 添加到项目中,我执行了以下过程:项目结构 > 库 > 添加 > Java > JavaFxPath/lib

然后我在这个过程中为输出jar文件创建了Artifect:项目结构 > Artifects > Add > JAR > From Modules with dependencies > main Class : main.Main.

解决方案

如果您有一个简单的(非模块化)JavaFX 11 项目(没有 Maven/Gradle 构建工具),并且您正在使用 IntelliJ,就像来自

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

现在你应该可以像这样运行它:

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

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

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

肥罐

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

转到<代码>文件->项目结构 ->文物 ->添加 ->JAR ->从具有依赖项的模块,添加您的主类,接受.

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

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

java -jar outartifactsHelloFX_jarHelloFX.jar

但这行不通.

原因 1:您需要一个启动器类,如

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

java -jar outartifactsHelloFX_jarHelloFX.jar

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

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

无论如何,您应该考虑使用 jlink .

注意

关于这个错误:

<块引用>

引起:java.lang.ClassNotFoundException: FilesJavajavafx-sdk-11.0.1lib

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

set PATH_TO_FX="C:Program FilesJavajavafx-sdk-11.0.1lib"

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

--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:

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 FilesJavajavafx-sdk-11.0.1lib
Caused by: java.lang.ClassNotFoundException: FilesJavajavafx-sdk-11.0.1lib

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

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

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

解决方案

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

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 Artifacts from IntelliJ.

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

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

where PATH_TO_FX has been set in File -> Settings -> Appearance & Behavior -> Path Variables, pointing to the JavaFX SDK lib.

Semi fat Jar

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

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

Then remove the JavaFX jars from the list, and accept.

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

Now you should be able to run it like:

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

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

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

Fat Jar

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

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

Then keep the JavaFX jars from the list, and accept. Build the project.

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

java -jar outartifactsHelloFX_jarHelloFX.jar

But this won't work.

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

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.

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

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

java -jar outartifactsHelloFX_jarHelloFX.jar

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

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.

Anyway, you should consider using jlink instead.

Note

About this error:

Caused by: java.lang.ClassNotFoundException: FilesJavajavafx-sdk-11.0.1lib

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:

set PATH_TO_FX="C:Program FilesJavajavafx-sdk-11.0.1lib"

这篇关于如何使用 JDK 11 打开 JavaFX .jar 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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