• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading resource from a war.

 
Chase Bonham
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file deployed under WEB-INF/classes/foo.txt.

I want to be able to read the file using MyClass.class.getClassLoader().getResourceAsStream("foo.txt");

It doesn't work.

Whats the best approach.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaikiran Pai:
Try this


Try using ; minor modification to Jaikiran's code.


 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The the getResourceAsStream provided by the class loader will look for your resource on the classpath.

For web applications, you should use the getResourceAsStream that is provided by the servletContext.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!