how to load the local files relative to the current java file in jwebbrowser

681 Views Asked by At

how would I load local files relative to the current java file in JWebbrowser? I know that I could load my page with navigate("path"); the problem is how to set relative path!

for example my java code is in : D:\Eclipse_Project\MyProject\src\javaCode\browser.java and the html file is in : D:\Eclipse_Project\MyProject\src\pages\html.html

but I don't want to use as follow:

webBrowser.navigate("file:///D:/Eclipse_Project/MyProject/src/pages/html.html");

Edit:

my html file also contains CSS an javaScript.

2

There are 2 best solutions below

8
On

Please try the following code:
1. Read html content with scanner.
2. Set html string to webBrowser.

String webContent = new Scanner(new File("src\\pages\\html.html")).useDelimiter("\\Z").next();
webBrowser.setHTMLContent(webContent);
0
On

If it's still actual, I had the same problem, this is the solution:

you need to use webserver like that:

webBrowser = new JWebBrowser();
webBrowser.navigate(WebServer.getDefaultWebServer().getClassPathResourceURL(getClass().getName(), "your_html_content.html"));