当我使用 getClass()。getResource(ACCEPT_PNG)加载ImageIcon时,它在我的本地计算上运行良好。 当我的类在JAR中嵌入其ressource时,对于Java Web Start应用程序,无法找到ressource,并且相同的代码返回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;
}
答案 0 :(得分:1)
I had the same issue and solved it by following the approach in Oracle's Java Web Start tutorial: use the class loader to retrieve the resource instead of the class itself:
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