我的第一个问题:
几天来我一直试图解决这个问题,但我已经失去了耐心。
以下是一些代码和我的项目结构。
问题:我怎样才能得到getResources()
在 eclipse 中工作并导入到 jar 后?
谢谢您的帮助。
public enum Icons {
XXX("src/resoruces/icons/xyz.png");
private ImageIcon icon;
Icons(String path) {
try {
// will fail miserably in eclipse and after exporting to jar
URL imageURL = getClass().getClassLoader().getResource(path);
icon = new ImageIcon(imageURL);
} catch (Exception e) {
// works like a char in eclipse and after creating the jar file
// with the files in the same directory
System.out.println("getResoruce() did not work");
icon = new ImageIcon(path);
}
}
通常,当从 Eclipse 导出 JAR 文件时,内容为src/resources
被出口minus the src
路径名本身。要从 JAR 中加载图像,您可以执行以下操作:
InputStream stream = getClass().getResourceAsStream("/resources/icons/xyz.png");
ImageIcon icon= new ImageIcon(ImageIO.read(stream));
确定的一种方法是检查:
jar tvf yourjar.jar
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)