需要帮助解决奇怪的 Class#getResource() 问题 [英] Need help with strange Class#getResource() issue
问题描述
我有一些从现有 jar 读取配置文件的遗留代码,例如:
I have some legacy code that reads a configuration file from an existing jar, like:
URL url = SomeClass.class.getResource("/configuration.properties");
// some more code here using url variable
InputStream in = url.openStream();
显然它以前工作过,但是当我执行此代码时,URL 有效,但我在第三行收到 IOException,说它找不到该文件.该 url 类似于file:jar:c:/path/to/jar/somejar.jar!configuration.properties
",因此它看起来不像是类路径问题 - java 很清楚在哪里可以找到文件..
Obviously it worked before but when I execute this code, the URL is valid but I get an IOException on the third line, saying it can't find the file. The url is something like "file:jar:c:/path/to/jar/somejar.jar!configuration.properties
" so it doesn't look like a classpath issue - java knows pretty well where the file can be found..
以上代码是蚂蚁任务的一部分,在任务执行时失败.
The above code is part of an ant task and it fails while the task is executed.
奇怪 - 我将代码和 jar 文件复制到一个单独的类中,它按预期工作,属性文件是可读的.
Strange enough - I copied the code and the jar file into a separate class and it works as expected, the properties file is readable.
在某些时候我将蚂蚁任务的代码更改为
At some point I changed the code of the ant task to
URL url = SomeClass.class.getResource("/configuration.properties");
// some more code here using url variable
InputStream in = SomeClass.class.getResourceAsStream("/configuration.properties");
现在它可以工作了 - 直到它在另一个实现了类似访问模式的类中崩溃..
and now it works - just until it crashes in another class where a similiar access pattern is implemented..
为什么它以前可以工作,为什么现在失败了?我目前看到的唯一区别是,旧版本是使用 Java 1.4 完成的,而我现在正在尝试使用 Java 6.
Why could it have worked before, why does it fail now? The only difference I see at the moment is, that the old build was done with java 1.4 while I'm trying it with Java 6 now.
解决方法
今天我在构建服务器上安装了 Java 1.4.2_19 并让 ant 使用它.令我完全沮丧的惊讶:问题消失了.在我看来,Java 1.4.2 可以处理这种类型的 URL,而 Java 1.6 不能(至少在我的上下文/环境中).
Today I installed Java 1.4.2_19 on the build server and made ant to use it. To my totally frustrating surprise: The problem is gone. It looks to me, that java 1.4.2 can handle URLs of this type while Java 1.6 can't (at least in my context/environment).
我仍然希望得到一个解释,尽管我面临着重写部分代码以使用表现更稳定的 Class#getRessourceAsStream 的工作......
I'm still hoping for an explanation although I'm facing the work to rewrite parts of the code to use Class#getRessourceAsStream which behaved much more stable...
推荐答案
ClassLoader.getResourceAsStream
的典型实现是:
public InputStream getResourceAsStream(String name) {
URL url = getResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
Class.getResource
和 getResourceAsStream
的行为彼此相同.
Class.getResource
and getResourceAsStream
behave the same as each other.
所以看起来要么您使用了一个奇怪且损坏的 ClassLoader
子类,要么您这边的测试中存在一些错误.
So it looks like either you are using a strange and broken subclass of ClassLoader
, or there is some mistake in the tests on your side.
这篇关于需要帮助解决奇怪的 Class#getResource() 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!