-1

I have just finished my first java project, however I can't seem to get it to build properly. It will load the initial fxml file that the whole calculator runs on, however when I try to add a holiday/break in the holiday/break menu a dialogpane is supposed to display so the user can fill information, but I keep getting a location not set error when I run the jar file in the cmd. It all works when I launch it in intellij but not in the jar.

This is how I have my files set up. I made a resource directory in the FinishedCalCulator project and marked it as the resource root

File Structure

This is how I set the Location of the FXML file into the loader. This works when I run it in Intellij but not in the jar file.

Method call

Project Structure Project Structure

Running through CMD Running through CMD

Invocation Target Exception/Location not set error Invocation Target Exception/Location not set error

I have looked around and found a few posts talking about putting all of the fxml files into a resource file and I did that and got it working again in Intellij, but it still will not display the dialog panes when I try to add them. I'm not really sure what I am doing wrong here. I don't think I fully understand what I am doing wrong. Any help would be great.

  • 4
    Your resource file is named `AddHolidayDialog.fxml` yet you are searching for it with `getClass().getResource("/addHolidayDialog.fxml")`. Notice the difference of case in the "A"? The "file system" of a JAR file is _case sensitive_. However, Windows' file system is not. Thus, it works when ran from Intellij but not from the JAR. – Slaw May 14 '18 at 08:53
  • 4
    Do not post screenshots of code. Post code as text formatted as code blocks instead. In this case the only additional info the screenshot provides is the name of the `getResource` method parameter and highlighting the occurances of `dialog`. This info is unimportant to the question though. The stack trace should be posted as text (code block) too instead of a screenshot; it should be simple enough to redirect the error stream to a text file and copy&paste it to the question... – fabian May 14 '18 at 10:47
  • Does this answer your question? [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) – kleopatra Mar 23 '23 at 05:27

1 Answers1

5

To expand on my comment...

Answer

In your code you are calling getResource("/addHolidayDialog.fxml"). Your resource's filename, however, is AddHolidayDialog.fxml. Simply change the "a" in your code to "A" and your code should work.


Explanation

Running from Intellij

When you run your application from Intellij it uses the output files in the out/production/classes directory. In other words, the classes are not in a JAR file. When you call getClass().getResource("/addHolidayDialog.fxml") you will get a URL like:

file://C:/.../out/production/classes/addHolidayDialog.fxml

Which works on Windows despite the fact the file is actually named AddHolidayDialog.fxml because Windows has a case-insensitive file system.

Running from JAR

Then you export your project to a JAR and run it from there. Calling the same getResource code you will now get a URL (if the code worked) like:

jar:file://C:/.../your-application.jar!/addHolidayDialog.fxml

What your code actually returns from getResource, however, is null. According to the JAR file, the resource addHoldiayDialog.fxml doesn't exist. It'll happily tell you that AddHolidayDialog.fxml does exist, however. The reason for this being that a JAR file has a case-sensitive "file system".

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Thank you it was that simple. I should have caught that, I didn't know windows would actually run it if the file name was wrong. Thanks for the information. – James Hatfield May 14 '18 at 17:14