This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference among different getResourceAsStream()?

 
Yan Zhou
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the difference among these three getResourceAsStream():

this.getClass().getResourceAsStream()
this.getClass().getClassLoader().getResourceAsStream()
Thread.currentThread().getContextClassLoader().getResourceAsStream()

Thanks.
Yan
 
Pedro Gongora
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the difference is on the classloader and methods that are used to find the resource.
Classloaders have a hierarchy, that is, they form some kind of tree (where context classloader is the root of the tree) and resources (classes or properties files) loaded into memory by some classloader might no be visible to their parent.
Also, they differ on where they search for the resource, for example, HttpServletRequest has a getResourceAsStream() method, that searches for resources under /WEB-INF either if webapp is "exploded" on the filesystem or packaged in a .war archive. Other getResourceAsStream() methods don't do that.
You have to read documentation for specific behaviour of each method.
Please, does anybody could confirm this ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic