java - getClass().getResource() 与 Java Web Start

标签 java java-web-start

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

相关文章:

java - Objective C 中的字符串加密

java - 在java中组合比较器

java - 对象实例化另一个对象?

java - 检测应用程序是否由 Webstart 启动的最佳方法是什么

java - BKM 向最终用户提供基于 Web 的 java 应用程序 + Jetty + JRE

java - Radius 协议(protocol) - 如何正确设置第二个请求中的状态?

java - 为什么这不修改以符合合规性?

macos - Java Web Start in 10.8.2 with native (Apple) Java 1.6 找不到 JRE

java - 如何从 web start 下运行的客户端获取 Java 堆栈跟踪?

web-services - 在 JBoss Web 服务中获取 IP 地址