• Post Reply Bookmark Topic Watch Topic
  • New Topic

confusion about getResourceAsStream in java.  RSS feed

 
Muztaba Hasanat
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get some configuration from properties file using getResourceAsStream. But it is returning null. Here is my code -



this code is not working. the inputStream is null.

and the project structure is like -



but if I change the code like



then it is working.

if I am not wrong, class and the properties file should be placed in a same package. But I have seen many other code that they only mentioned application.properties in getResourceAsStream.

Now the question is, if it is obvious to mention the package name also if the class and file in not the same package or is there any other way to read file properties file to read with getResourceAsStream method.

Thank you.
 
Salil Wadnerkar
Ranch Hand
Posts: 92
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to be careful to distinguish between


and


because the two versions work differently. That's likely why your web search produced confusing or conflicting answers, because the answers were describing different code fragments.

My preference is to use the second version (because the way it works is documented, unlike the first version). If your resource is in the same folder as the class, then the code I posted will do the job. But it appears from your earlier post that isn't the case. So I think what you need is this:



Notice that it uses an absolute URL and not the relative URLs that you have been trying.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!