Solved

Listing resources

Posted on 2006-06-16
6
287 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 84 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 83 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 83 total points
ID: 16922959
you may have to consider overriding the class loader and look for files in diff location!
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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