vrijdag 12 augustus 2011

getClass().getResource(filename) doesn't work if file is still in jar

I was moving an application from Tomcat to Weblogic and by default in Tomcat the jar/war is extracted whereas in Weblogic this isn't at least not in our setup.

So my code to read a file within my applications classpath was not longer working.

initial code:
URL input = getClass().getResource(queryFileName);
File file = new File(input.getFile());

However the file is not null it's not readable and throws a FileNotFoundException

new code:
InputStream stream;
stream = getClass().getResourceAsStream(queryFileName);

The only disadvantage is you have an InputStream instead of a file.