Issue
I have created a new javafx project with a standard initial code.
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Created a jar artifact and built it. After that, I checked this jar via the command line and it runs.
If I run it through a double click, then it gives an error.
Solution
Compared to Swing applications where you can easily collect all artifacts in IDEA and make a runnable jar file, JavaFx applications work in another way. and you can make a runnable jar file but with difficulties because JavaFx uses modules.
You can use launch4j to create a .exe
file. As for me, I prefer to use JPackage to create a native installer. To use it you can launch the cmd line and write a script, something like:
jpackage -t exe --name (name of your application) --description "(your own discription)" --app-version 1.0 --input (location of jar file)--dest (destination of exe/msi file) --icon (path of icon) --main-jar(name of your jar file) --module-path (location of your jmode)--add-modules
here is my example:
jpackage -t exe --name calculatorFx --description "Simple Calculator Author:Me" --app-version 0.1 --input C:\Users\PC\Desktop\calculator_JAR --dest C:\Users\PC\Desktop\calculator_JAR --icon C:\Users\PC\Desktop\calculator_JAR\Calculator.ico --main-jar calculatorFx.jar --module-path C:\JavaFX\javafx-jmods-11.0.2 --add-modules javafx.controls,javafx.fxml,java.desktop,javafx.base,javafx.graphics --win-shortcut --win-menu
here is a useful video https://www.youtube.com/watch?v=m31BxwXJiV8
If you are using Windows you are required to download WiX Toolset from the official site https://wixtoolset.org/releases/v3-11-1-2318 .
And if you require additional modules for your project other than java.desktop
(this module is included in the jdk), you must download jmodules
from gluonhq and put them in some folder, and in the script above you have to specify the location of this module.
Answered By - Albus_Percival
Answer Checked By - Mildred Charles (JavaFixing Admin)