0

I'm trying to style a JavaFX WebEngine using the setUserStyleSheetLocation method. The method's documentation says it requires a "local URL" starting with data:, file:, jar:, or jrt:. I want to package my project in a runnable JAR file, but I don't know where to put my web.css file nor what to pass into the setUserStyleSheetLocation method in order for my app to detect the CSS file at runtime.

My project structure is:

- OuterProjectFolder
  - src
    - pack
      - Main.java
    - resources
      - web.css

but I can move web.css if needed.

Given the above placement of web.css, I have tried passing all of the following literal strings to setUserStyleSheetLocation, to no avail:

file:web.css
jar:web.css
file:resources/web.css
jar:resources/web.css
file:/resources/web.css
jar:/resources/web.css
file:src/resources/web.css
jar:src/resources/web.css

and I have also tried passing:

Main.class.getResource("/resources/web.css").toString()
Main.class.getResource("/resources/web.css").toExternalForm()

To create my JAR, I am using Eclipse's File > Export... > Runnable JAR File with "Library handling" set to "Package required libraries into generated JAR." I have double-checked that the CSS file is actually in my JAR. My app is able to load other things from my resources folder such as images and even other CSS files (like the one used to style my Scene).

Any help would be sincerely appreciated.

Sam Hooper
  • 187
  • 8
  • What happened when you tried `Main.class.getResource("/resources/web.css").toString()`? – Slaw Sep 19 '21 at 23:10
  • 1
    Also, this Q&A may help: [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). – Slaw Sep 19 '21 at 23:10
  • 1
    @Slaw When I use `Main.class.getResource("/resources/web.css").toString()`, I get an `IllegalArgumentException: Invalid stylesheet URL` from `WebEngine`. Calling`getResource(...).toString()` returns a string starting with `rsrc:`, which is not listed as one of the allowable prefixes in the docs for `setUserStyleSheetLocation`. Looking at the code called by `setUserStyleSheetLocation`, it seems that those prefixes (`data:`, `file:`, etc.) are literally enforced by a string comparsion. Thanks for the help. – Sam Hooper Sep 20 '21 at 02:36
  • 1
    Yeah, I don't know why they're so strict. But the `rsrc:` scheme is a consequence of how Eclipse packages runnable JARs with "package required libraries into generated JAR" enabled. In a "normal" JAR file, `getResource` would be giving you a `file:jar:` scheme. – Slaw Sep 20 '21 at 03:15

0 Answers0