使用超链接从.jar在SWT浏览器中打开HTML文件 [英] Opening HTML files in SWT browser from .jar with hyperlinks working

查看:193
本文介绍了使用超链接从.jar在SWT浏览器中打开HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为SWT应用程序提供了一组帮助文件,这些文件已使用Browser控件在应用程序中打开.在帮助文件中的导航是通过相对路径名的超链接完成的(即:<a href="aboutUs.htm">,因此Java代码helpHome.htm实际上只打开了一个html文件.我正在使用String homeURL = this.getClass().getResource("/help/helpHome.htm").toString();browser.setURL(homeURL);来打开它)当我只是在Eclipse中调试时,不幸的是,当我将项目移至.jar时,浏览器给出了标准的找不到此网页"错误. 此链接,该链接可用于helpHome.htm,但是当我单击一个超链接,它使我进入显示相对路径名的空白页面,是否有一种方法可以说服浏览器使用setURL(String)方法从可执行jar中打开html文件?类似的结果?

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?

提前谢谢!

推荐答案

您必须按照

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.

然后设置browser.setURL(homeURL).现在,所有后续的超链接都将指向服务器,该服务器知道如何为请求的资源提供服务.

Then set the browser.setURL(homeURL). All the subsequent hyperlinks will now point to the server, which knows how to serve the requested resources.

在eclipse中运行或调试应用程序时,超链接将解析为文件系统路径(例如file://C:\ workspace....),然后一切正常.但是,当您从可运行的jar中运行应用程序时,情况并非如此.

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.

这篇关于使用超链接从.jar在SWT浏览器中打开HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆