开发者

getClass().getResource() with Java Web Start

开发者 https://www.devze.com 2023-02-19 01:08 出处:网络
When I use getClass().getResource(ACCEPT_PNG) to load an ImageIcon, it works well on my local compute.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消