• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing jar file contents  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a java program, which I want to export into a jar file.
Bundled into the jar file, is a folder called 'images'.

If I just wanted to access a single image bundled into the jar, I would do:


But I actually want to look inside the images folder.
If I did:

Then this would look outside of the jar file for the 'images' folder.
But I want to look at the 'images' folder inside the jar file, and then examine its contents.

I'm not sure if I need to use JarFile or not, and if so, then I guess the program would not execute from IDE, but would only work after JAR-ing it.

Can anyone advise?
Thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here an example of how to extract a set of images from the icons folder stored in a Jar file named runme.jar located on the c:\ directory.

 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is another example that reads all images in the jar file and displays them in a Window:



I hope that helps!
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent stuff, thanks!

I've changed my design, and will be accessing my images in an external folder to the jar, so I won't need to use this this time.

But I will definatley use it on another project, I don't understand this...

And

I've never seen that before, I thought it would just be "C:/something".

Thanks
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:/something is a valid filename, indeed (since Java doesn't care about / or \).

However, a URI or URL needs a protocol. This could be http for the WWW, ftp, mailto for email addresses, or file for local files. This protocol is followed by a colon and some slashes, dependant on the protocol. http and ftp require two (for http:// and ftp://), mailto zero (e.g. mailto:you@yourdomain.com), and file usually takes two as well.

So, for a local file URL or URI, you get file:// followed by the file path.


Now entries within a JAR file (and a ZIP file as well!) can be directly pointed to, using the special JAR protocol. This is inserted before the regular protocol. Then, at the end of the URI / URL that points to the JAR file, you add a !, followed by the absolute name of the entry.

So if your JAR file's URI is "file://C:/my.jar", the URI for the manifest file would be "jar:file://C:/my.jar!/META-INF/MANIFEST.MF". I haven't tried, but it should work for JARs on a webserver as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!