Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getResourceAsStream not loading resource in webapp

I have a web application that uses a library which resides in TOMCAT_HOME/common/lib. This library looks for a properties file at the root of the classpath (in a class called ApplicationConfig):

ApplicationConfig.class.getResourceAsStream("/hv-application.properties");

My Tomcat web application contains this properties file. It is in WEB-INF/classes, which is the root of the classpath right? However, at runtime, when it tries to load the properties file, it throws an exception because it can't find it (getResourceAsStream returns null).

Everything works fine if my application is a simple, standalone Java application. Does Tomcat cause the getResourceAsStream method to act differently? I know there's a lot of similar questions out there, but none of them have helped unfortunately. Thanks.

like image 581
Michael Avatar asked Apr 16 '10 13:04

Michael


1 Answers

Try Thread.currentThread().getContextClassLoader().getResourceAsStream("/hv-application.properties") instead.

like image 194
Binil Thomas Avatar answered Sep 28 '22 10:09

Binil Thomas