Solved

Listing resources

Posted on 2006-06-16
6
281 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:
mayankeagle 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
wordmultiple challenge 12 92
maven project error 5 24
Java DateChooser? 3 14
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 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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now