Attempting to load a local page in JavaFX webEngine

I have a webView component in the tab of my JavaFX application that I am trying to load a locally saved HTML page:

WebView browser = new WebView(); WebEngine webEngine = browser.getEngine(); webEngine.load("/webView/main.html"); 

My html document (possibly wrong) is stored in the following location:

The location of my HTML document

where com.cds.gui contains the class in which I am trying to upload a file. If I print webEngine.getDocument() , I get null - that is, the document does not load.

Please let me know where I am going wrong! Thanks.

+7
source share
3 answers

You need to read the local file as a URL so that WebEngine can find it. For example, you can find a file as a resource using

 URL url = this.getClass().getResource("/com/cds/gui/webView/main.html"); webEngine.load(url.toString()); 

Or you can load the actual String path into a File object and use it to get the string URL.

 File f = new File("full\\path\\to\\webView\\main.html"); webEngine.load(f.toURI().toString()); 

Hope this helps!

+17
source

You can use the file syntax for the URI, for example.

 file:///C:/path/to/file.html (Windows) 

https://en.wikipedia.org/wiki/File_URI_scheme

+2
source

I suffered for a long time with the paths to the file, and it works for me (Maven project, folder resources):

 WebEngine engine = html.getEngine(); File f = new File(getClass().getClassLoader().getResource("html/about.htm").getFile()); engine.load(f.toURI().toString()); 
0
source

Source: https://habr.com/ru/post/1244130/


All Articles