• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting a resource in a web container

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to read a file which I have put under WEB-INF/lib folder. But a I don't want to use a hardcoded reference to that file. I'm using getResource() method on ClassLoader. But i'm a getting a null reference. I have configured the server to use only one classloader for the entire application. To be on a safe side, I'm trying to get the resource on all classloaders like this:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = null;
while (true) {
url = loader.getResource("filename");
loader = loader.getParent();
if(url != null || loader == null) {
break;
}
}
if(url!=null) {
FileReader reader = new FileReader(url.getFile());
}

But still, it is not able to find the file.

Is there anything wrong with my approach?
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to load a resource in a jar file? If not you can try to put the file in a jar file. If will be put into the classpath and then it will become available using the classloader (getResource).
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manuel Moons:
Are you trying to load a resource in a jar file? If not you can try to put the file in a jar file. If will be put into the classpath and then it will become available using the classloader (getResource).



Yes if I do that, it is able to locate the file. But it is giving the file path as /WEB-INF/lib/files.jar!/config.xml and the FileReader is throwing an exception. files.jar is where I put the file "config.xml".

But if i put the file under WEB-INF/classes it is working fine.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes, you can also put it in the classes folder (I forgot about that, it's been a long time since I developed a webapp).

You can also use the getResourceAsStream method. You do not have to use a FileInputStream in that case since you already have a stream open to that file. It's just an idea, but that will work for both the resource in the jar and in the classes folder.
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the getResourceAsStream() is working fine. Thanks!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic