0

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.

enter image description here

If I run it through a double click, then it gives an error.

enter image description here

  • What build tool are you using? – SedJ601 Aug 22 '22 at 17:30
  • in intellij idea, in the project structure tab, I created a jar artifact, added everything I needed from javafx to it, and in the build tab, I just created it. I did everything according to this video https://www.youtube.com/watch?v=F8ahBtXkQzU @SedJ601 – Oleg Boldov Aug 22 '22 at 17:51
  • 2
    I'm surprised it runs via the command line, as typically you need to specify the location of the JavaFX modules. Is that your default JDK? What happens if you just do `java -jar texter.jar`? – James_D Aug 22 '22 at 18:06
  • same thing, this is my default jdk @James_D – Oleg Boldov Aug 22 '22 at 18:08
  • 2
    Is windows configured to run the same command (java -jar) on double click? See https://stackoverflow.com/questions/8511063/how-to-run-jar-file-by-double-click-on-windows-7-64-bit – slindenau Aug 22 '22 at 18:58
  • open the jar file you created and see (show) the contents of the manifest file (we are interested in whether the `Main-Class` attribute is specified correctly) – mr mcwolf Aug 23 '22 at 05:47
  • Main-Class: com.texter.Main @mrmcwolf – Oleg Boldov Aug 23 '22 at 09:01
  • from what you have shown it is clear that the class is `HelloApplication` (package name is not visible), not `com.texter.Main`. specified the attribute correctly, and if everything else is fine the application will start normally. – mr mcwolf Aug 23 '22 at 10:43
  • @mrmcwolf My guess is they have a separate main class that doesn't extend `javafx.application.Application`. This is a necessary workaround/hack when creating a fat/uber JAR that includes JavaFX (JavaFX will refuse to start if the main class extends `Application` and JavaFX is on the class-path instead of the module-path, i.e., if `javafx.graphics` is not found in the boot layer, and everything in a fat/uber JAR ends up on the class-path). – Slaw Aug 23 '22 at 17:52
  • @Oleg I would look up how Windows handles "double-click to launch" functionality, then see what is currently configured for JAR files. That `ClassNotFoundException` error you're getting is very strange indeed. It seems to think the file path to your JAR file is the name of the class, which doesn't make sense. – Slaw Aug 23 '22 at 17:56
  • 1
    @Slaw yes i think so as well. like i linked in my previous comment, it looks like the double click action for `.jar` is set to execute `java %` and not `java -jar %`. For example if i run the command `java bla.jar` on my machine the result is `Error: Could not find or load main class bla.jar` – slindenau Aug 25 '22 at 07:35

1 Answers1

0

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.

slindenau
  • 1,091
  • 2
  • 11
  • 18
  • It's preferable to deploy JavaFX applications as self-contained applications (e.g., via `jpackage`), but it is still possible to deploy as an executable JAR file. It's also still possible to deploy JavaFX on the class-path, though you'll get a warning about it being unsupported (I'm not aware of anything that breaks as of JavaFX 18, however). You can see this warning in OP's output from the version that "works". – Slaw Aug 22 '22 at 20:47
  • 1
    please take the time to type real English words (vs fakes like _U_) .. and format the cmd line as code to make it easily readable – kleopatra Aug 22 '22 at 23:09
  • launch4j is not used to create installers but native launchers for windows. For other platforms, such a decision is pointless. – mr mcwolf Aug 23 '22 at 05:42
  • I created the installer, launched it. It worked, but did not create an application shortcut, and now when I try to run the installer, it opens and closes instantly. @Albus_Percival – Oleg Boldov Aug 23 '22 at 08:56
  • @OlegBoldov if you are use Windows try to download WiX Toolset in official site https://wixtoolset.org/releases/v3-11-1-2318/ . After installing WiX Toolset try to launch your installer again and look for shortcut. – Albus_Percival Aug 23 '22 at 17:51
  • @OlegBoldov and if you are required an additional modules for your project other than java.desktop (this module included in jdk) you must download jmodules from gluonhq : https://download2.gluonhq.com/openjfx/11.0.2/openjfx-11_windows-x64_bin-jmods.zip – Albus_Percival Aug 23 '22 at 18:05
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 26 '22 at 16:19