0

I am trying to export a JavaFX program that I wrote with Intellij into a JAR so that I can send it to my classmates so they can run it during an oral presentation.
My files are structured this way :
src

  • main
    • java
      • plop
        • main
        • others classes and controllers
    • resources
      • plop
        • the three FXML files

To access the FMXL I use this :

53        FXMLLoader fxmlLoader = new FXMLLoader(main.class.getResource("SceneOuverture.fxml"));
54        Parent root =fxmlLoader.load();
55        Scene scene = new Scene(root);

When I run it from Intellij, it works, however when I export it with this artifact it stops working :

<component name="ArtifactManager">
  <artifact type="jar" name="modified-boids:jar">
    <output-path>$PROJECT_DIR$/out/artifacts/modified_boids_jar</output-path>
    <root id="archive" name="modified-boids.jar">
      <element id="directory" name="META-INF">
        <element id="file-copy" path="$PROJECT_DIR$/src/META-INF/MANIFEST.MF" />
      </element>
      <element id="module-output" name="modified-boids.main" />
      <element id="extracted-dir" path="$PROJECT_DIR$/../../../../../../Program Files/Java/jdk-18.0.1.1/lib/src.zip" path-in-jar="/" />
      <element id="extracted-dir" path="$PROJECT_DIR$/../../../../../../Program Files/Java/jdk-18.0.1.1/lib/jrt-fs.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/18.0.1/3285f1855bf294dd4eba57ba84f96c555c819260/javafx-base-18.0.1-win.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/18.0.1/79b7b4ecf6a90692e4accdbf9e4e68714d235844/javafx-graphics-18.0.1-win.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/18.0.1/be9bf95be2716c11b88594eeaf4b832874265cc9/javafx-base-18.0.1.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/18.0.1/22cf4d2bbb80d0d0430fcf71cdf0dfe3475ebd93/javafx-controls-18.0.1-win.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.kordamp.bootstrapfx/bootstrapfx-core/0.4.0/8d019b30266c00770cf29921d1008ad7feed0ed6/bootstrapfx-core-0.4.0.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/18.0.1/4180a4bfda9692cbb444fb70b2506546e03f961b/javafx-graphics-18.0.1.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-fxml/18.0.1/c38102123668cca2a7dea83a7567fbebbe93f148/javafx-fxml-18.0.1-win.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.controlsfx/controlsfx/11.1.1/d303c563c23d5176346e8d05932afbec421060ae/controlsfx-11.1.1.jar" path-in-jar="/" />
      <element id="extracted-dir" path="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/18.0.1/c292e8fcec3ffd843216a29c3b00b2d320efe83d/javafx-controls-18.0.1.jar" path-in-jar="/" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-console-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-console-l1-2-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-datetime-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-debug-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-errorhandling-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-file-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-file-l1-2-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-file-l2-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-handle-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-heap-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-interlocked-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-libraryloader-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-localization-l1-2-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-memory-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-namedpipe-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-processenvironment-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-processthreads-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-processthreads-l1-1-1.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-profile-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-rtlsupport-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-string-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-synch-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-synch-l1-2-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-sysinfo-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-timezone-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-core-util-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-conio-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-convert-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-environment-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-filesystem-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-heap-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-locale-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-math-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-multibyte-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-private-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-process-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-runtime-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-stdio-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-string-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-time-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/api-ms-win-crt-utility-l1-1-0.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/decora_sse.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/fxplugins.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/glass.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/glib-lite.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/gstreamer-lite.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/javafx_font.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/javafx_iio.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/jfxmedia.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/jfxwebkit.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/msvcp140.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/msvcp140_1.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/msvcp140_2.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/prism_common.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/prism_d3d.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/prism_sw.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/ucrtbase.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/vcruntime140.dll" />
      <element id="file-copy" path="$PROJECT_DIR$/../../javafx-sdk-19/bin/vcruntime140_1.dll" />
    </root>
  </artifact>
</component>

This is the error message I have when I launch the JAR :

Exception in Application start method    
(...)      
Caused by: java.lang.IllegalStateException: Location is not set.             
(...)      
at plop.main.start(main.java:54)

I have tried to replace the three lines with these two :

53        Parent root =FXMLLoader.load(Objects.requireNonNull(getClass().getResource("SceneOuverture.fxml")));              
54        Scene scene = new Scene(root);

However it still doesn't work so I have opened the JAR with 7-Zip and I can't find the FXML files in the ones that are extracted from the JAR, so I don't really know if the FXML cannot be extracted with 7-Zip or if they are not exported at all.
Do you have an idea of what I should do?

A.CAL
  • 41
  • 6
  • Did you really call it “ressources” or is that just a typo in the question? By default, build tools will copy the content of the “resources” folder to the build. – James_D Nov 03 '22 at 22:12
  • It was a typo :) – A.CAL Nov 03 '22 at 22:17
  • This is maven/gradle standard directory structure. Are you using a build tool? (you should be, IMO). If Maven, the `mvn package` command will create the jar with resources for you (you can run it from the IDE if you want). For sharing with your classmates, you could check the project into a shared git repo, then they could check it out and build and run it from source in an IDE the same as you are doing. Or you can export the jar, but then they also need all of the java runtime and javafx modules and other supporting modules. A javafx-maven-plugin jlink zip may be easier (I advise that). – jewelsea Nov 03 '22 at 23:58
  • I don't advise trying to use IDE specific artifact export functions ([like this](https://www.jetbrains.com/help/idea/working-with-artifacts.html#build_artifacts)) to build sharable jars for JavaFX apps. I don't think, as they are currently setup, those functions really work to do such a task. – jewelsea Nov 04 '22 at 00:00
  • The main problem is that I am a biology student, I don't think any of my classmates knows what git is so I am searching for an easy solution for them to use the app. I have Gradle in my IDE, but I don't really understand how it works. I have added in the build.gradle :sourceSets{ main{ resources{ srcDir("src/main/resources") } } } However when I build it and run it with Gradle I have this message : Execution failed for task ':run'. > Process 'command 'C:\Program Files\Java\jdk-18.0.1.1\bin\java.exe'' finished with non-zero exit value 1 – A.CAL Nov 04 '22 at 10:54
  • Maven is probably a bit easier to use than Gradle (just my opinion). I recommend using the [javafx-maven-plugin jlink zip](https://github.com/openjfx/javafx-maven-plugin#javafxjlink-options). An example configuration is in the link. – jewelsea Nov 04 '22 at 21:53

1 Answers1

1

To put the fxml files in the jar, the solution is to add the resources folder in the artifact as "directory content".
Then, I had to replace
Parent root=FXMLLoader.load(Objects.requireNonNull(getClass().getResource("SceneOuverture.fxml"))) with Parent root =FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/plop/SceneOuverture.fxml"))) for every FXML loader.

A.CAL
  • 41
  • 6