wood burning stoves 2.0*
The moose likes Applets and the fly likes Extracting Images from the image jar  file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Extracting Images from the image jar  file" Watch "Extracting Images from the image jar  file" New topic

Extracting Images from the image jar file

shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
In oreder to improve the performance of my applet I want to download the images as a jar file (say images.jar).
But when I tried to read the images from this jar file I am getting an exception
like this.
java.util.zip.ZipException: No such file or directory
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:112)
at java.util.jar.JarFile.<init>(JarFile.java:127)
at java.util.jar.JarFile.<init>(JarFile.java:65)

Here is few lines from my code which is trying to extract images from the jar.

JarFile jarfile = new JarFile("images.jar");

ZipEntry firstentry = jarfile.getEntry("images/xyz.gif");
int size= (int) firstentry.getSize();
The exception is thrown from the first line ( "JarFile jarfile = new JarFile("images.jar")").

In the archive attribute of my applet tag I am adding this jar file also..

<APPLET CODE = "abc.xyz.class" CODEBASE = "." ARCHIVE = "abc.jar,images.jar" ...

Please help me.

The greatest pleasure in life is doing what peoples say you can not do.
Joe Ess

Joined: Oct 29, 2001
Posts: 8876

The JarFile constructor takes the name of a local file. This fails, of course, because your jar file is back on the server. You may be able to load the images individually through Class.getResourceAsStream() since the jar is in the classpath (well, ARCHIVE parameter). If that doesn't work you will have to create a URL back to the JAR file and use JarInputStream to load it.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
Thanks for ur reply ..

I solved the problem using 'JarURLConnection' class.


wood burning stoves
subject: Extracting Images from the image jar file