Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx WebEngine load html file from jar

I am building a project and need to use WebEngine class. So I have 2 projects, MainA and SecondB. The SecondB project is in dependency to MainA, so after building it SecondB becomes a jar file in the MainA libs folder.
Now. I need to open a abc.html file that is under SecondB resources. When testing it locally it works, when building an app and deploying it on server it fails (probably because it is in SecondB jar file). So the code I am using is:

WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();  
ClassLoader classLoader = getClass().getClassLoader();
String s = classLoader.getResource("subfolder/abc.html").toExternalForm();
webEngine.load(s);

The method classLoader.getResource("subfolder/abc.html").toExternalForm();returns a normal url when running the code localy and something like:

file:jar:C:/Something/MainA/libs/SecondB.jar!/subfolder/abc.html

Do you have any ideas how to load this file from a jar? I tried several options I found on SO, but without success

like image 863
kamil.rak Avatar asked Apr 20 '26 02:04

kamil.rak


1 Answers

You can create a "resources" package and copy your files to this package and for accessing your file path for example an html you can use this: getClass().getResource("/resources/abc.html").toURI().toString() this provides to get your files url in your created jar. Hope it is useful.

like image 190
GltknBtn Avatar answered Apr 28 '26 21:04

GltknBtn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!