When I use getClass().getResource(ACCEPT_PNG) to load an ImageIcon, it works well on my local compute. When my class is embeded with its ressource in a JAR, for a Java Web Start application, the ressource can't be found, and the same code returns null...
Any idea?
/** Path to a PNG ressource. */
private static final String ACCEPT_PNG = "accept.png";
private static ImageIcon acc开发者_Python百科eptPngIcon = null;
private ImageIcon getAcceptPngIcon() {
if (acceptPngIcon == null) {
acceptPngIcon = new ImageIcon(getClass().getResource(ACCEPT_PNG));
}
return acceptPngIcon;
}
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
精彩评论