P
Philipp
Hello
Inside a jar, how can you get a listing of a folder?
(File.list() does not work)
Thanks Phil
Inside a jar, how can you get a listing of a folder?
(File.list() does not work)
Thanks Phil
Philipp said:Hello
Inside a jar, how can you get a listing of a folder?
(File.list() does not work)
Thanks Phil
Steve said:You need to visit the API documentation and look more closely at how
this works. A JAR file is *not* a "filesystem", so you can't treat its
contents as java.io.File instances. Instead, it's a file itself with
multiple entries. Look at java.util.jar.JarFile and you'll see that it
has methods for iterating over JarEntry items it contains. And you'll
need to remember that a "folder" inside a jar file is yet another entry,
even if the unzipped contents include more files or folders there.
Philipp said:....
Yes I was aware of that. I still have a problem:
- How to get a reference to the actual running jar file?
Andrew said:Perhaps it is better to investigate why you believe you
need to get a list of files that were (supposedly) put there
by you in the first place.
What is the actual end purpose of this exercise?
What does it bring to the end user?
Philipp said:OK I have a tool in my application which has some factory presets which
are offered to the user. These presets are written in XML and stored in
the jar at /resources/presets/
Now I don't want to hard code each name of the (many) factory presets.
How should I do this cleanly?
My scenario is also interesting, because the user can build his own
presets and save them somewhere in a folder on his file system (not in
the jar!).
Andrew said:- Use a logical sequence for the file names
(e.g. 1.preset.xml to 299.preset.xml) store the
highest number xml name in a file with a known name.
(e.g. /resources/presets/total.dat).
- Use ant to build the project and write a list of
the xml preset files into a list of known name.
- Extend that to a web-start based (1.5+) launch
with lazy downloads (assuming these files are big),
and test for each (sequentially named) presets file
using the JNLP API.
- Compact the 299 preset files into a single file of
all presets, with a known name.
Philipp said:Hello and thank you for your answer.
In fact you are telling me that there is no clean way to do what I want
to do... :-(
I would say that the way you are trying to do this is not clean
(although, admitedly, some of Andrew's suggestions are not either).
Jar files are not really meant to be 'random access filesystems'.
If you files are XML files, then is there a compelling reason why they
cannot be merged into a single file with multiple <preset> elements (as
per Andrew's last suggestion)? This would be much cleaner, I think.
My original question was more "can it be done my way" (ie did I miss
something in the docs).
Andrew said:Philipp wrote:
...
Well. Now that we have discussed some of the alternate
(and perhaps altogether better) strategies*, I can confirm
that it *is* possible to get a list of the contents of a jar
file, then simply enumerate it to find entries of interest.
java.util.jar.JarFile.entries()
java.util.zip.ZipEntry.getName()
> why the
application jar does not know what it expects to find
inside itself. That factor alone, suggests a potential
design flaw.
Philipp said:Hello and thank you for your answer.
In fact you are telling me that there is no clean way to do what I want
to do... :-(
I will have to switch to ant building then...
Phil
Steve said:I don't think he told you that at all...though I may not fully
understand all of what he did tell you.
For accessing things that are in your jar file, you don't need to know
where the jar is. You can access them as "resources". For example, in
my app's jar file I have some images which we use as "icons" of a sort
in a specific package -- call it com.company.icons for simplicity. In
my code, I can access any of them by using the com/company/icons
notation for their location. The same kind of thing can be done for
other kinds of resources -- property files, embedded data files, etc.
For things that are not inside the jar file, assuming you have
appropriate privileges, you can often make a URL or URI referring to
their location (file:// kind of thing) and read them in that way once
you know where they are.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.