Class.getResource()和ClassLoader.getResource()在可执行jar中的奇怪行为 [英] Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar

查看:117
本文介绍了Class.getResource()和ClassLoader.getResource()在可执行jar中的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 Class之间有什么区别.getResource()和ClassLoader.getResource()?
和自己的代码,

I understand from What is the difference between Class.getResource() and ClassLoader.getResource()? and from own code, that

getClass().getResource("/path/image.png")

getClass().getClassLoader().getResource("path/image.png")

发布无法读取jar文件中的图像显示使用

getClass().getClassLoader().getResource("path/image.png")

可执行jar文件中的

返回null,而

in an executable jar file returns null, while

getClass().getResource("/path/image.png")

返回正确的URL。

自Class。删除前导斜杠后,getResource()委托给 ClassLoader.getResource()我希望这些调用是相同的,但显然它们不是在这种情况下。即使特殊类加载器附加到特定类,每次调用它仍然应该是相同的,再次导致相同的行为。

Since Class.getResource() delegates to ClassLoader.getResource() after removing the leading slash, I would expect that these calls are identical, but obviously they are not in this case. Even when a special class loader is attached to the particular class, it should still be the same one for each call, again resulting in the same behavior.

所以,问题是:是否有任何明显的情况下,以下
代码在第一次调用时返回null但是第二次调用的正确URL?

package com.example;

import java.net.URL;

public class ResourceTest {

   public void run() {
      URL iconUrl1 = getClass().getClassLoader().getResource("path/image.png");
      System.out.println("ClassLoader.getResource(\"path/image.png\"): " + iconUrl1);

      URL iconUrl2 = getClass().getResource("/path/image.png");
      System.out.println("Class.getResource(\"/path/image.png\"): " + iconUrl2);
   }

   public static void main(String[] args) {
      ResourceTest app = new ResourceTest();
      app.run();
   }
}


推荐答案

我以为这个问题已经被问到并回答了!

I thought this question was already asked and answered!


getClass()。getResource()相对于.class的搜索文件,而
getClass()。getClassLoader()。getResource()相对于
类路径根进行搜索。

getClass().getResource() searches relative to the .class file while getClass().getClassLoader().getResource() searches relative to the classpath root.

如果这里有SSCCE,我不明白为什么它没有

If there's an SSCCE here, I don't understand why it doesn't

1)显示目录.jar中的组织,......

1) Show the directory organization in the .jar, and...

2)考虑包裹

问:什么(如果有的话)还没有得到答案 Class.getResource()和ClassLoader.getResource()有什么区别?(以及它引用的链接)?

Q: What (if anything) hasn't already been answered by What is the difference between Class.getResource() and ClassLoader.getResource()? (and the links it cites)?

=================================== ======================================

=========================================================================

我仍然不确定什么不清楚,但这个例子可能会有所帮助:

I'm still not sure what isn't clear, but this example might help:

/*
  SAMPLE OUTPUT:
  ClassLoader.getResource(/subdir/readme.txt): NULL
  Class.getResource(/subdir/readme.txt): SUCCESS

  ClassLoader.getResource(subdir/readme.txt): SUCCESS
  Class.getResource(subdir/readme.txt): NULL
 */
package com.so.resourcetest;

import java.net.URL;

public class ResourceTest {

    public static void main(String[] args) {
        ResourceTest app = new ResourceTest ();
    }

    public ResourceTest () {
        doClassLoaderGetResource ("/subdir/readme.txt");
        doClassGetResource ("/subdir/readme.txt");
        doClassLoaderGetResource ("subdir/readme.txt");
        doClassGetResource ("subdir/readme.txt");
    }

    private void doClassLoaderGetResource (String sPath) {
        URL url  = getClass().getClassLoader().getResource(sPath);
        if (url == null)
            System.out.println("ClassLoader.getResource(" + sPath + "): NULL");
        else
            System.out.println("ClassLoader.getResource(" + sPath + "): SUCCESS");
    }

    private void doClassGetResource (String sPath) {
        URL url  = getClass().getResource(sPath);
        if (url == null)
            System.out.println("Class.getResource(" + sPath + "): NULL");
        else
            System.out.println("Class.getResource(" + sPath + "): SUCCESS");
    }
}

这是相应的目录树。它恰好是一个Eclipse项目,但无论是Eclipse,Netbeans还是.jar文件,目录都是相同的:

Here's the corresponding directory tree. It happens to be an Eclipse project, but the directories are the same regardless if it's Eclipse, Netbeans ... or a .jar file:

C:.
├───.settings
├───bin
│   ├───com
│   │   └───so
│   │       └───resourcetest
│   └───subdir
└───src
    ├───com
    │   └───so
    │       └───resourcetest
    └───subdir

正在打开的文件是subdir / readme.txt

The file being opened is "subdir/readme.txt"

ADDENDUM 11/9/12:

ADDENDUM 11/9/12:

嗨 -

我从github逐字复制你的代码,重新编译并重新运行:

I copied your code verbatim from github, re-compiled and re-ran:

ClassLoader.getResource(/subdir/readme.txt): NULL
Class.getResource(/subdir/readme.txt): SUCCESS
ClassLoader.getResource(subdir/readme.txt): SUCCESS
Class.getResource(subdir/readme.txt): NULL

如果那是你得到的输出......我很困惑。

If that's not the output you're getting ... I'm baffled.

无论它值多少,我都在跑:

For whatever it's worth, I'm running:


  • Eclipse Indigo(应该没关系)

  • Eclipse Indigo (it shouldn't matter)

在IDE内部运行(无论是文件系统还是.jar,在IDE内部或外部都无关紧要)

Running inside the IDE (it shouldn't matter if it's filesystem or .jar, inside or outside an IDE)

我的JRE是1.6(如果有的话,这可能是个大问题)

My JRE is 1.6 (if anything, this is probably the biggie)

很抱歉,我们无法解决我的想法这是一个简单的问题:(

Sorry we haven't been able to resolve what I thought was a straightforward issue :(

ADDENDUM 11/21/12(Andreas):

ADDENDUM 11/21/12 (Andreas):

由于此问题最近没有活动,我想总结一下我们发现:

Since there was no recent activity on this question, I would like to summarize what we found:


  • 根据我们的共识,上述问题的答案是:不,不可能 Class.getResource(/ path / image.png)返回有效的URL,而 ClassLoader.getResource(path / image.png) 返回null


    • 我们完全清楚ClassLoader.getResource()和Class之间的区别。 getResource()

    • 我们的示例输出匹配,对于SUCCESS和null

    • 样本输出符合我们的预期

    • 结论:要么我们监督某事,要么不同导致链接问题中描述的解决方案起作用。我想我们目前无法证明这一个。

    • From our common understanding, the answer to the above question is: "No, it is not possible that Class.getResource("/path/image.png") returns a valid URL, while ClassLoader.getResource("path/image.png") returns null":
      • We're completely clear on the difference between ClassLoader.getResource() and Class.getResource()
      • Our sample outputs match, for both "SUCCESS" and for "null"
      • The sample outputs match what we'd expect
      • Conclusion: Either we oversaw something, or something different caused the "solution" described in the linked question to work. I think we can not currently prove one or the other.

      这篇关于Class.getResource()和ClassLoader.getResource()在可执行jar中的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆