boycod3
boycod3

Reputation: 5319

image not loading in javafx css from resource folder while making jar

project 
  src/main/java
  src/main/resources  css & img

Hi above is my maven based javafx app structure. The app not loading images from runnable jar file. example -fx-background-image: url("../img/bg.PNG");

Error loading image: jar:file:/D:/demo.jar!/img/top_bg.PNG

Upvotes: 1

Views: 1982

Answers (1)

jewelsea
jewelsea

Reputation: 159290

Things to Check

Double check that the file you are looking for is actually in the jar file you are attempting to run and that it is situated at the location you expect (run jar tvf <yourjarfile.jar> to check this).

Also, double-check to make sure you haven't made a typo, for instance you mention url("../img/bg.PNG") but the error message you display is top_bg.PNG, so the filename in the error message isn't matching the file name in your example.

Additionally, from the JavaFX CSS reference:

Note that for inline styles, leading dot-segments (e.g. '..' or '.') do resolve since the path is always anchored at the root of the classpath.

So, such notation will only work inside a stylesheet, not in an inline style.

Aside

Normally, don't use .. when trying to retrieve resources from a jar file.

Unlike http: and file: protocols, the jar: protocol does not resolve relative parent address containing ...

I did do some testing locally though and the CSS implementation in JavaFX appeared to resolve the .. specifiers in URLs for resources sourced out of a jar file, so that does not appear to be the issue here. Probably the CSS implementation is preprocessing the .. to remove it from the url before passing it to the url loader to load the resource. (I checked the source code for the JavaFX CSS parser, and that is what it does, so .. is fine when used in a CSS stylesheet sourced from a jar).

Upvotes: 1

Related Questions