java jar 资源_java – 如何访问JAR文件中的资源?

我有一个带有工具栏的

Java项目,工具栏上有图标.这些图标存储在名为resources /的文件夹中,例如路径可能是“resources / icon1.png”.这个文件夹位于我的src目录中,所以当它被编译时,文件夹被复制到bin /

我正在使用以下代码来访问资源.

protected AbstractButton makeToolbarButton(String imageName, String actionCommand, String toolTipText,

String altText, boolean toggleButton) {

String imgLocation = imageName;

InputStream imageStream = getClass().getResourceAsStream(imgLocation);

AbstractButton button;

if (toggleButton)

button = new JToggleButton();

else

button = new JButton();

button.setActionCommand(actionCommand);

button.setToolTipText(toolTipText);

button.addActionListener(listenerClass);

if (imageStream != null) { // image found

try {

byte abyte0[] = new byte[imageStream.available()];

imageStream.read(abyte0);

(button).setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(abyte0)));

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

imageStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} else { // no image found

(button).setText(altText);

System.err.println("Resource not found: " + imgLocation);

}

return button;

}

(imageName将是“resources / icon1.png”等).这在Eclipse中运行时工作正常.但是,当我从Eclipse导出可运行的JAR时,找不到图标.

我打开了JAR文件,资源文件夹在那里.我已经尝试了所有的东西,移动文件夹,更改JAR文件等,但我无法获得显示的图标.

有谁知道我做错了什么?

(作为一个侧面的问题,有没有可以使用JAR文件的文件监视器?当路径问题出现时,通常只需打开FileMon即可看到发生了什么,但是在这种情况下它只是显示为访问JAR文件)

谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值