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.

4
  • What happened when you tried Main.class.getResource("/resources/web.css").toString()? Commented Sep 19, 2021 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?. Commented Sep 19, 2021 at 23:10
  • 1
    @Slaw When I use Main.class.getResource("/resources/web.css").toString(), I get an IllegalArgumentException: Invalid stylesheet URL from WebEngine. CallinggetResource(...).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. Commented Sep 20, 2021 at 2: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. Commented Sep 20, 2021 at 3:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.