I have a Java webapp WAR file that depends on multiple jars in it's WEB-INF\lib directory. One of these JARS needs to load some config files by doing class.getClassLoader().getResourceAsStream(...)
. However the InputStream resturns null. Is there a problem with taking this approach when the JAR is inside a WAR? The app is deployed on Tomcat6.
EDIT MORE INFO:
I'm tring to load in SQL queries from files so I can run them. These are located in a separate DAO jar within the web app's WAR, under WEB-INF/lib
mywebapp.war
-- WEB-INF
-- lib
-- mydao.jar
---- com/companyname/queries
-- query1.sql
-- query2.sql
-- query3.sql
...
CODE USING TO LOAD CLASSES
public class QueryLoader {
private static final Logger LOGGER = Logger.getLogger(QueryLoader.class.getName());
public String loadQuery(String fileName) {
final String newline = "\n";
BufferedReader reader = new BufferedReader(new InputStreamReader(
QueryLoader.class.getClassLoader().getResourceAsStream(
"/com/companyname/queries/" + fileName)));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(newline);
}
} catch (IOException e) {
LOGGER.error(e);
}
I have also tried changing the getResourceAsStream line to
Thread.currentThread().getContextClassLoader().getResourceAsStream(
without success.
My development environment is MS Windows Vista and but I encounter the same error when running it on this environment and on Ubuntu.