This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ClassLoader and directories

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to load all of the files inside a directory that is inside a war or jar? I have been using the ClassLoader to load config files which works great if you know the name of the file.
URL url = this.getClass().getClassLoader().getResource("config/manufacturerDataImport/import1.xml");
However I would like to be able to load all of the files inside the directory, and have the names be able to change. So If I had a URL that is
URL url = this.getClass().getClassLoader().getResource("config/manufacturerDataImport/");
I want to get a Collection of all of the xml files inside that URL directory.
sharp shooter, and author
Posts: 1913
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and for WAR files there are two ways. First of all you can use the getRealPath() method on the ServletContext object althought this isn't recommended as it doesn't work too well!
The best way (that I've found) is to use the ServletContext.getResources() method. I use this in my TagUnit testing framework to find a list of all test*.jsp files underneath a certain directory. Here's a snippet from TagUnit to help you on your way...

Hope that's useful! I've tested this code in WebLogic 7, Websphere 5, Tomcat 4, Resin 2, Jetty (recent version) and it works a treat. However, it doesn't work with OC4J/Oracle 9i at the moment.
For JAR files, you have to use the classes in java.util.jar - I also do this in TagUnit so I'll try to dig out some code for you...
[ March 21, 2003: Message edited by: Simon Brown ]
He was expelled for perverse baking experiments. This tiny ad is a model student:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic