• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading resource from a war.  RSS feed

 
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.
 
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
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.


 
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)
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!