Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

java - How to open JavaFX .jar file with JDK 11?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

SemiJar

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

Fat Jar

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...