使用Java Web Start获取getClass()。getResource()

时间:2011-03-23 15:20:20

标签: java java-web-start

当我使用 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;
   }

1 个答案:

答案 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