Skip to Main Content

FXMLLoader issues with resources in jar file

jerry kramskoyOct 28 2016 — edited Oct 30 2016


I am having a problem with my application finding resources when the classes and resources are all in one jar file.  Up till now, I have just been using Eclipse without a jar and all works fine.

Now I need to use a jar file for deployment to OSX.

But with the jar, it fails to find resources which I can see are present in the jar.

I'm debugging this in Eclipse, giving the VM args      -jar ./webcontent.jar javafx.main.StartEmuso

For example: 

intervalColourPicker = FXMLLoader.load(getClass().getResource("/res/fxml/intervalColourPicker.fxml"));

This loads the fxml and a class is instantiated as per the controller class mentioned in the fxml.  But that fxml also includes an ImageView, with the image url given as

  <ImageView id="closeicon" fx:id="intervalcolourpickerdone" fitHeight="15.0" fitWidth="15.0" layoutX="363.0" layoutY="3.0" onMouseClicked="#intervalcolourpickerdone" pickOnBounds="true" preserveRatio="true">


        <Image url="@../artwork/closefile.JPG" />



In, the url is jar:file:/C:/androidwood/emJFX/webcontent.jar!/res/artwork/closefile.JPG 

/res/artwork/closefile.JPG is indeed the correct path to the JPG in the jar file.  But the Image.validateURL(..) throws an exception when it can't find the image.

I note that it uses

Thread.currentThread().getContextClassLoader() rather than getClass().

I tried changing my code to use this in the 1st line above, but makes no difference.

Any insight here would be much appreciated.

Thank you, Jerry

Post Details
Added on Oct 28 2016