0

I recently finished my first JavaFX project and am ready for deployment. I found that when I create the jar file for my project my ide creates a folder with the jar and other necessary files. I noticed that when I run the jar in the file everything works fine. However, when I take the jar our of that folder and place it as a desktop icon, various resources no longer become available - I am assuming this is because the jar file and the various resource files are no longer in the same file path/folder. - Is this the correct assumption to make?

Regardless, I wanted to ask what is the standard method of getting JavaFX resources and the accompanying jar file to work when the resources are not located in the same folder?

Essentially, I want to have a clickable desktop icon that launches the app, which the jar file fulfills. But if I put that jar file in a folder with its resources to get the project to work properly then the user will have to press the folder and then the jar file in order to get the project to launch - which is very counterintuitive.

Any ideas on how this issue is handled?

  • For packaging, as you want a clickable desktop icon to launch your application, consider an installer via [`jpackage`](https://www.baeldung.com/java14-jpackage) created using a modern distribution (JDK and JavaFX 19+) rather than a `jar`. JavaFX 8 is obsolete and most of your clients won't already have that installed. The installer can ensure an appropriate runtime is available and setup the desktop shortcut for the application. – jewelsea Jan 10 '23 at 00:09
  • 2
    For resource layout and deployment: see [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) and the [eden resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/). – jewelsea Jan 10 '23 at 00:10
  • What do you mean by "resources"? If you mean things like images, CSS files, FXML files, and so on, then those should be _embedded_ in your JAR file. If you mean dependencies, well that gets a little more complicated, given you're using JavaFX. You can try creating a so-called fat/uber JAR file, but JavaFX 9+ doesn't technically support that setup. Though you could require your clients to have a JRE-with-JavaFX installed. Personally, I think the best way to deploy a JavaFX application is with a modern version of Java so you can use `jpackage` (images and the like should still be embedded). – Slaw Jan 10 '23 at 01:15
  • If you're really using Java/JavaFX 8, then there may be a tool named `javafxpackager` / `javapackager` that you can use (that tool no longer exists in newer versions of Java and was replaced with `jpackage`). – Slaw Jan 10 '23 at 01:16
  • If your current setup works fine, do not take the jar out of the folder. Right-click on the jar and create a shortcut. Move the shortcut to the desktop so that the users can click on it to start the app. – SedJ601 Jan 10 '23 at 04:46

1 Answers1

0

The best way to do this is to create a shortcut to your jar file not copy it to another location. The jar file depends on these resources to execute especially if you used external libraries. The other alternative would be to export your jar file with the libraries included in the jar. This however would make your jar very huge depending on the number of libraries you have. I hope this helps.

Benson Kiprono
  • 129
  • 1
  • 1
  • 12