Need help with strange Class#getResource() issue

Posted by Andreas_D on Stack Overflow See other posts from Stack Overflow or by Andreas_D
Published on 2010-01-14T16:53:16Z Indexed on 2010/05/31 3:02 UTC
Read the original article Hit count: 281

Filed under:
|

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

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.

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

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.

Workaround

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

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

© Stack Overflow or respective owner

Related posts about java

Related posts about ant