• Post Reply Bookmark Topic Watch Topic
  • New Topic

listing files in a jar file  RSS feed

 
Steffen Reinhard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been fiddling about with jar files, lately... Currently I'm using a .jar that contains a number of resources such as fonts, images and so on.

As long as I attempt to access a resource (e.g. using getClass().getResource(String s) for images or getClass().getResourceAsStream(String s) for setting up of a custom font, respectively) things work properly.

I tried to create a new File object using a path (say, File f = new File("/images")). Now, calling f's isDirectory() method returns 'false'.
Same thing is happening when I call f's exists() method, though I get a friendly getAbsolutePath() message confirming a path for a file that obviously doesn't exist:


The output is:

Absolute Path: /images
/images exists? false
/images is a directory? false

What I need is a List (or an Array) of filenames to access resources programmatically without having to look up the fienames/paths myself.

Is there any way to list the files contained in a given directory whithin the jar's internal file structure?

Confusedly yours,

Steffen
[ December 21, 2006: Message edited by: Steffen Reinhard ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not by using File; File represents... well, files.

To discover the contents of a jar file, you need to use Java's jar file libraries in package java.util.jar. For example, look at the class JarFile and its method "entries".
 
Steffen Reinhard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your prompt reply to my question which might solve my problem though I admit I haven't yet figured out how to work with the java.util.jar classes.

My problem seems to be a little more complex than I conceived in the first place as I chose to embed the jar containing utility classes and resources in the jdk1.6.0 directory (to be more specific, at jre/lib/ext),
but I expect locating the jar in question should be possible by using System properties such as JAVA_HOME or even JRE_HOME as well as standard File-class functionality.

Once more thank you ever so much for your help as well as your patience.

Cheers,


Steffen
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!