Using JavaFX and Eclipse IDE, I have used the sample app from Teamdev for loading a sample file (notifications.html from my file system) but I keep getting the page not found/DNS error.
The file in question, notifications.html, is right there in the same package as the source file that invokes it as shown in the snippet below:
Scene scene = new Scene(new BorderPane(view), 700, 500);
primaryStage.setScene(scene);
primaryStage.show();
browser.loadURL("notifications.html");
I think my issue is composing the fully qualified path and since I'm using a Mac, it is not clear to me how to do this. I have tried:
browser.loadURL("Users/myusername/Documents/workspace/jxBrowser/src/application/notifications.html");
However it did not work.
You need to use loadHtml()
instead loadUrl()
suppose loadHtml goes to network and you try to download url from file on your pc. Read html from filesystem to String and pass it to loadHtml()
method.
InputStream urlStream = getClass().getResourceAsStream("/notifications.html");
String html = null;
try (BufferedReader urlReader = new BufferedReader(new InputStreamReader (urlStream))) {
StringBuilder builder = new StringBuilder();
String row;
while ((row = urlReader.readLine()) != null) {
builder.append(row);
}
html = builder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
browser.loadHTML(html);
for this code your html file must be in resources
folder of your project