且构网

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

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

更新时间:2022-10-14 23:00:49

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



构建项目,它会创建一个相当的小jar(在这种情况下为3 KB)。



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

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

(确保%PATH_TO_FX%指向有效的文件夹并使用如果它包含空格则引用。



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



Fat Jar



如果你想要一个包含JavaFX依赖项的全脂jar,你仍然可以使用工件。



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



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



理论上,您应该能够像以下一样运行它:

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

但是这不起作用。



原因1:你需要一个启动器类,如上所述



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

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

您可以分发此jar,但仅限于Windows平台。



如果你下载他们的JavaFX SDK,你可以为其他平台创建类似的jar,你也可以构建跨平台orm jar如果你把它们全部加在一起,如上面的链接答案中所解释的那样。



无论如何,你应该考虑使用 jlink 而不是



注意



关于此错误:


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


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

 设置PATH_TO_FX =C:\Program Files \ Java \ javafx-sdk- 11.0.1 \lib


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

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 out\artifacts\HelloFX_jar\HelloFX.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 out\artifacts\HelloFX_jar\HelloFX.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 out\artifacts\HelloFX_jar\HelloFX.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: 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:

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