11

I have a webView component on a tab in my JavaFX application which I am trying to load an locally stored HTML page into:

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

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

Location of my HTML document

where com.cds.gui contains the class where I am attempting to load the file. If I print out webEngine.getDocument() I get null - i.e. the document isn't getting loaded.

Please let me know where I'm going wrong! Thanks.

TomRaikes
  • 340
  • 1
  • 6
  • 18
  • 2
    Assuming this code is in a class in the `com.cds.gui` package, try `webEngine.load(getClass().getResource("webView/main.html").toExternalForm());` (note no leading `/` on the path). – James_D Feb 29 '16 at 19:53
  • @James_D This is exactly what I was looking for, thanks! You should have posted this as an actual reply. :) – Rapti Nov 17 '20 at 13:02
  • @Rapti See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – James_D Nov 17 '20 at 13:14

3 Answers3

25

You need to read the local file in as a URL so that the WebEngine can find it. For instance, you can find the file as a resouce 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!

Marian
  • 431
  • 4
  • 6
  • Thanks for your answer, I tried to read the file as a URL but I still get a null pointer exception? – TomRaikes Feb 29 '16 at 16:43
  • Do you actually see any content loaded on the WebView itself? – Marian Feb 29 '16 at 16:51
  • No nothing, on the HTML page itself I just have the text '123abc' within paragraph tags – TomRaikes Feb 29 '16 at 16:56
  • So you want to print the Document object which usually takes a little longer to get a value set. You can add a ChangeListener on the documentProperty of the engine which notifies you when the Document value changes from null to an object instance. – Marian Feb 29 '16 at 17:01
  • Also, try to use the package name as part of the path to the file if you haven't done that already. Like this: getResource("/com/cds/gui/webView/main.html"); – Marian Feb 29 '16 at 17:13
  • Loading it as a file makes no sense. What happens when you package your application into a jar file? – James_D Feb 29 '16 at 19:50
  • Well, if it's prepackaged with the jar then use the getResource() method just make sure you have the right relative path. The file thing can be used when browsing to local html files and loading them if they are not in the jar @James_D. – Marian Feb 29 '16 at 20:08
  • Exactly: in this particular case though the file is clearly part of the application (the OP shows it in the source code). So the resource mechanism is the appropriate method. – James_D Feb 29 '16 at 20:54
3

Long tormented with the paths to the file, and this 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());
2

You could use the file syntax for the URI e.g.

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

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

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • 2
    As this application is going to be deployed on many different systems, this isn't really an option from what I have learnt. However if there is a way of getting the current location of the class (as the file is contained in a folder within the current project) please let me know!! – TomRaikes Feb 29 '16 at 16:49
  • Perhaps something along these lines?: https://community.oracle.com/thread/3519040?start=0&tstart=0 – ManoDestra Feb 29 '16 at 16:54
  • I am trying `String url = Main.class.getResource("/webview/main.html").toExternalForm();` and am getting a null pointer exception. – TomRaikes Feb 29 '16 at 17:00
  • Shouldn't have to, but try from the root of the classpath: /com/cds/gui/webView/main.html. And note the capital V in "webView" (according to your structure above). – ManoDestra Feb 29 '16 at 20:09