Solved

Listing resources

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
ejb on wildfly 5 46
Bot application - advice 3 60
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 27
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 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