0

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.

user90
  • 73
  • 3
  • 13

2 Answers2

0

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"));
Denis
  • 503
  • 8
  • 32
-1

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);
eric
  • 261
  • 1
  • 4
  • 11
  • I try it, but it didn't run load the page correctly! – user90 Aug 05 '14 at 14:08
  • Does your html.html is pure html file without any javascript and css local dependent files? I've tried that it should work, make sure your relative path is correct. By default "user.dir" should be your project working directory. – eric Aug 05 '14 at 14:27
  • It contains javascript and css! – user90 Aug 05 '14 at 15:21
  • In that case, I guess you need to make sure your html.html should import the right path of js and css files with a relative path. – eric Aug 06 '14 at 01:52
  • Please paste your html.html out. – eric Aug 06 '14 at 07:22
  • I think I find the problem, in my javascript code I need to load some picture which is located in src/images/pic.png. so how should I address it in my html.html file so that it could find it?! – user90 Aug 06 '14 at 08:01
  • try to use "../images/pic.png" or just <%=request.getContextPath()%>/... Anyway, that's a problem of relative path, just print some log and reset path will easy to work, I think. – eric Aug 06 '14 at 08:14
  • sorry that I ask to much question! `"../images/pic.png"` and `<%=request.getContextPath()%>../images/pic.png` didn't work! – user90 Aug 06 '14 at 08:35