Reputation: 43987
I am running an application from a servlet container. This application produces letters which are derived from some file templates that packed with the jar. I get hold of these files by Class.getResourceAsStrem
and the application works perfectly fine.
However, after thousands of invocations, the method suddently returns null
and continues to return null
until the application is restarted. I have no explanation for this and I cannot see a reason why this would suddenly happen.
I first thought this might be because of unclosed streams pointing to the same resource but I am wrapping the stream processing in a catch
-try
-block right after opening it. Also, a heap dump does not show such stream objects and I furthermore think that this should result in an IOException
instead. Also, it says in the javadoc
A InputStream object or null if no resource with this name is found
But why would the application suddenly not longer be able to find a resource it successfully found before.
Any ideas?
Upvotes: 1
Views: 304
Reputation: 43987
I think I found the reason. ClassLoader.getResourceAsStream(String)
to which Class.getResourceAsStream
eventually delegates is implemented like this:
public InputStream getResourceAsStream(String name) {
URL url = getResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
I guess there is an IOException
occurring and that there are indeed leaking open files. However, this exception gets swallowed and I get a null
pointer in return. Genius API...
Update: After looking at a heap dump, I found application code that was leaking files. Damn you, ClassLoader
implementation, misdirecting me with NullPointerException
s.
Upvotes: 1