Solved

Listing resources

Posted on 2006-06-16
6
283 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 40
login jsp example 24 47
eclipse apache tomcat admin console 52 96
Error in @AspectJ Based AOP with Spring 2 13
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

776 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