Java getResourceAsStream JAR inside WAR

I have a Java webapp WAR file that depends on several jars in this WEB-INF \ lib directory. One of these JARS needs to load some configuration files by running class.getClassLoader().getResourceAsStream(...) . However, an InputStream returns zero. Is there a problem with this approach when the JAR is inside the WAR? The application is deployed on Tomcat6.

EDIT MORE INFO:

I am trying to load SQL queries from files so that I can run them. They are located in a separate DAO bank in the WAR web application, in the WEB-INF / lib section

 mywebapp.war -- WEB-INF -- lib -- mydao.jar ---- com/companyname/queries -- query1.sql -- query2.sql -- query3.sql ... 

EXTERNAL CLASS USE CODE

 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 also tried changing the getResourceAsStream line to

 Thread.currentThread().getContextClassLoader().getResourceAsStream( 

without success.

My development environment is MS Windows Vista, but I encounter the same error when I run it in this environment and on Ubuntu.

+4
java tomcat deployment
source share
3 answers

Managed to get it working with Spring Resource Loader

 public String loadQuery(String fileName) { final String newline = "\n"; ApplicationContext ctx = new ClassPathXmlApplicationContext(); Resource res = ctx.getResource("classpath:/com/msi/queries/" + fileName); BufferedReader reader; StringBuilder sb = new StringBuilder(); try { reader = new BufferedReader(new InputStreamReader(res.getInputStream())); String line; while ((line = reader.readLine()) != null) { sb.append(line); sb.append(newline); } } catch (IOException e) { LOGGER.error(e); } return sb.toString(); 

}

+3
source share

It should work, but you need to make sure that you use the correct class loader.

+2
source share

Assuming you're not making a rookie mistake by putting QueryLoader in another JAR, the only problem I see is that you are using File.separator but showing up (from your use \ ) using Windows. When using getResourceAsStream delimiter is always a slash ( / ), as if you were using a URL.

If I change this, I get the following:

 QueryLoader.class.getClassLoader().getResourceAsStream( "/com/companyname/queries/" + fileName) 

Of course, if QueryLoader is in the com.companyname.queries package (along with the queries themselves), you should simply do this:

 QueryLoader.class.getResourceAsStream(fileName) 

Just. (It states that Class.getResourceAsStream qualifies relative file names with the name of the containing package.)

+2
source share

All Articles