Basically the problem is that to work with relative links in html you need the full url, including the scheme, etc. A URL related to the current class or working directory will not work.
Assuming that the html file and the image associated with it are associated with the application (i.e. when you create the jar file for the application, the html file and image will be part of the jar file), you can get the URL for the html file with
getClass().getClassLoader().getResource("path/to/file.html");
where the path refers to the class path. Then you can use toExternalForm() to convert to String in the appropriate format. This is suitable for html man pages etc.
Here is an example:
HTMLTest.java:
package htmltest; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.WebView; import javafx.stage.Stage; public class HTMLTest extends Application { @Override public void start(Stage primaryStage) throws Exception { WebView webView = new WebView(); webView.getEngine().load(getClass().getClassLoader().getResource("htmltest/html/test.html").toExternalForm()); Scene scene = new Scene(webView, 600, 600); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
test.html:
<html> <head><title>Test</title></head> <body> <h1>Test page</h1> <img src="img/testImage.png"/> </body> </html>
testImage.png:

Project Layout:
htmltest - HTMLTest.class - html - test.html - img - testImage.png
Screenshot:

On the other hand, if you really download the HTML file from the file system, you can create a File object for the HTML file and then convert it to a URI. This would be appropriate, for example, if you wrote an HTML editor in which the user edited the HTML file and saved it in the file system, and you wanted to display the result in a web view; or, if you suggested the user upload an HTML file using FileChooser .
The code for this will look like this:
File htmlFile = new File(...); // or get from filechooser... webEngine.load(htmlFile.toURI().toString());
source share