需要用奇怪的类的getResource#帮助()问题 [英] Need help with strange Class#getResource() issue

查看:131
本文介绍了需要用奇怪的类的getResource#帮助()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些遗留的code读取从现有的罐子的配置文件,如:

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();

显然,这工作过,但是当我执行此code,URL是有效的,但我得到一个IOException在第三行,说它无法找到该文件。网址是像文件中:jar:C:/path/to/jar/somejar.jar configuration.properties ,所以它看起来并不像一个类路径问题 - java的知道pretty以及所在的文件可以发现..

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..

以上code是一个Ant任务的一部分,同时执行任务失败。

The above code is part of an ant task and it fails while the task is executed.

很奇怪 - 我复制了code和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.

在某些时候,我改变了Ant任务的code到

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并提出蚂蚁使用它。令我完全令人沮丧的惊喜:这个问题已经一去不复返了。在我看来,了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).

我仍然希望解释,虽然我现在面临改写code部分为使用类#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 的行为彼此相同。

所以看起来要么你正在使用的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.

这篇关于需要用奇怪的类的getResource#帮助()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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