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.
Main.class.getResource("/resources/web.css").toString()?Main.class.getResource("/resources/web.css").toString(), I get anIllegalArgumentException: Invalid stylesheet URLfromWebEngine. CallinggetResource(...).toString()returns a string starting withrsrc:, which is not listed as one of the allowable prefixes in the docs forsetUserStyleSheetLocation. Looking at the code called bysetUserStyleSheetLocation, it seems that those prefixes (data:,file:, etc.) are literally enforced by a string comparsion. Thanks for the help.rsrc:scheme is a consequence of how Eclipse packages runnable JARs with "package required libraries into generated JAR" enabled. In a "normal" JAR file,getResourcewould be giving you afile:jar:scheme.