• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

Traversing a directory inside WAR

I deploy an application in an unexploded war. I know how to get an input stream from a resource if I know the file name inside the war. However, is there a way to read a directory from the resource to get the names of files in the directory?
For example, if I have a directory like "/WEB-INF/classes/x", I can get the URL for that, but I don't know how to get the file names inside directory x.
  • 2
1 Solution
RogueCarAuthor Commented:
After some playing around, I came up with this:
          URL theFolderUrl = Z.context.getResource("/WEB-INF/classes/x");
          if (theFolderUrl == null) return;
          javax.naming.directory.DirContext jndd = (javax.naming.directory.DirContext)theFolderUrl.getContent();
          if (jndd == null) return;
          Enumeration e = jndd.list(""); // get all the files

This gives an enumeration of all the files in the directory.
RogueCarAuthor Commented:
Another note: the enumeration returns javax.naming.NameClassPair objects.  To get the file name, invoke the "getName()" method.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now