问题
I have a set of help files for my SWT application that I have open in-application using the Browser
control. Navigation through the help files is done through hyperlinks of relative pathnames (i.e: <a href="aboutUs.htm">
, so only one html file is actually opened by java code, helpHome.htm. I am opening this using String homeURL = this.getClass().getResource("/help/helpHome.htm").toString();
and browser.setURL(homeURL);
This works beautifully when I'm just debugging it in Eclipse. Unfortunately, when I move the project into a .jar, the browser gives the standard "can't find this webpage" error. I've tried using the browser.setText(String);
function as described in this link, which works for helpHome.htm, but when I click a hyperlink, it brings me to a blank page displaying the relative pathname. Is there a way to convince browser to open an html file from an executable jar using the setURL(String)
method? If not, are there any suggested workarounds for me to achieve similar results?
Thanks in advance!!
回答1:
You have to start an internal Server as explained here on any available port on your application start up and make your html files available as static resources to the server.
Then set the browser.setURL(homeURL)
. All the subsequent hyperlinks will now point to the server, which knows how to serve the requested resources.
The hyperlinks are resolved to File System Path (Ex. file://C:\workspace....) while you are running or debugging your application in eclipse and things were working fine then. But that is not the case when you run your app from a runnable jar.
来源:https://stackoverflow.com/questions/17099562/opening-html-files-in-swt-browser-from-jar-with-hyperlinks-working