当我使用 getClass().getResource(ACCEPT_PNG) 加载 ImageIcon 时,它在我的本地计算上运行良好。 当我的类与其资源一起嵌入到 JAR 中时,对于 Java Web Start 应用程序,无法找到资源,并且相同的代码返回 null...
有什么想法吗?
/** Path to a PNG ressource. */
private static final String ACCEPT_PNG = "accept.png";
private static ImageIcon acceptPngIcon = null;
private ImageIcon getAcceptPngIcon() {
if (acceptPngIcon == null) {
acceptPngIcon = new ImageIcon(getClass().getResource(ACCEPT_PNG));
}
return acceptPngIcon;
}
最佳答案
我遇到了同样的问题,并按照 Oracle 的 Java Web Start 中的方法解决了它 tutorial : 使用类加载器而不是类本身来检索资源:
getClass().getClassLoader().getResource(ACCEPT_PNG);
// works both locally and via Web Start
getClass().getResource(ACCEPT_PNG);
// only works locally; returns null for any path via Web Start
关于java - getClass().getResource() 与 Java Web Start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5407492/