jar文件 html,jar文件加載JEditorPane中的html頁面

I'm trying to load html pages stored inside the jar file into a help JEditorPane. So far it works when I run it in eclipse but when i make a runnable jar it wont work, except if i put the map res/pages/... in the same map with the jar file

我正在嘗試將存儲在jar文件中的html頁面加載到幫助JEditorPane中。到目前為止,當我在eclipse中運行它時,它工作,但是當我制作一個可運行的jar它不會工作,除非我把地圖res / pages / ...放在與jar文件相同的地圖中

class HelpButtonHandler implements ActionListener{

@Override

public void actionPerformed(ActionEvent arg0) {

infodex = new JEditorPane();

helpDialog = new JDialog();

URL url1 = null;

try {

url1 = (new java.io.File("res/pages/help.html")).toURI().toURL();

} catch (MalformedURLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

infodex.setPage(url1);

} catch (IOException e) {

e.printStackTrace();

}

helpDialog.getContentPane().add(new JScrollPane(infodex));

helpDialog.setBounds(400,200,700,600);

helpDialog.show();

infodex.setEditable(false);

Hyperactive hyper = new Hyperactive();

infodex.addHyperlinkListener(hyper);

}

}

3 个解决方案

#1

4

A file packaged inside a .jar is not a file on the file system. You cannot access it with the File class.

打包在.jar中的文件不是文件系統上的文件。您無法使用File類訪問它。

A file inside a .jar is called an application resource. You access it using the Class.getResource method:

.jar中的文件稱為應用程序資源。您可以使用Class.getResource方法訪問它:

url1 = HelpButtonHandler.class.getResource("/res/pages/help.html");

It is up to you to make sure the files are properly packaged in your .jar. If url1 is null, check the structure of your .jar file.

您可以確保將文件正確打包在.jar中。如果url1為null,請檢查.jar文件的結構。

#2

1

When you put resources in a jar, you cannot access them using File. You need to access them as a resource through the (more precisely: a) classloader. For example:

將資源放入jar時,無法使用File訪問它們。您需要通過(更確切地說:a)類加載器將它們作為資源訪問。例如:

HelpButtonHandler.class.getResource("/res/pages/help.html");

Make sure you put the resource in the right place: if you leave out the first slash ('/'), the classloader will try to locate it relative to your class (which is usually not what you want).

確保將資源放在正確的位置:如果省略第一個斜杠('/'),類加載器將嘗試相對於您的類(通常不是您想要的)找到它。

#3

0

use gerResource() method...

使用gerResource()方法......

url = getClass().getClassLoader().getResource("res/pages/help.html");

check this link

檢查此鏈接

http://oakgreen.blogspot.com/2011/12/java-getclassgetclassloadergetresourcem.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值