Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Listing resources

Posted on 2006-06-16
6
Medium Priority
?
290 Views
Last Modified: 2011-10-03
Hi!

Im writing an app that needs to load a number of xml files from a data directory. The app will be packaged in a jar which will contain a data directory with some xml files already in. The app will also look for a data directory external to the jar and load any xml files it finds in there too.

I use the following line of code to retrieve a URL to the data directory:

Enumeration<URL> dataURLs =
                    this.getClass().getClassLoader().getResources("data");

When I was debugging the app (not in a jar) I had the data directory on the local file system and the following code worked for getting the files in the directory:

URL url = dataURLs.nextElement();
File dir = new File(url.getPath());
String[] files = dir.list();

However, once its all jared up this code does nothing and none of the xml files are loaded.

So, my question is: How do I list files that may or may not be inside a jar given a URL to the directory they are contained in?

Cheers folks!
0
Comment
Question by:Cheney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 336 total points
ID: 16920444
When it is in the JAR, you need to perhaps load it as a resource and not as a file. Use getResourceAsStream ()
0
 
LVL 2

Author Comment

by:Cheney
ID: 16920664
Fair enough. The main problem is that I do not know how many files/resources there are or what they are called, only that they end in .dat, are in a directory named "data", and contain xml tags.
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 332 total points
ID: 16920932
As far as I know, you have two choices:

1)  Create a list of resources in the jar file, and put this list as a text file into a known place of the JAR.  Then, you can read this first, and read the resources from that
2)  Load the JAR as a JarFile (http://java.sun.com/j2se/1.4.2/docs/api/java/util/jar/JarFile.html), and list the entries in it using that class

You will of course need to know where the jar file is for the second option, which may prove tricky :-(

Tim
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 332 total points
ID: 16922959
you may have to consider overriding the class loader and look for files in diff location!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question