views:

144

answers:

4

I have this code on an applet. The applet works ok, but I get a lot of unnecessary duplicate download. In particular, I have noticed that each "getResource" triggers a download of the .JAR file.

static {
    ac = new ImageIcon(MyClass.class.getResource("images/ac.png")).getImage();
    dc = new ImageIcon(MyClass.class.getResource("images/dc.png")).getImage();

//...other images }

How can this be avoided?

A: 

Only a workaround:

You could put your images in a zip file inside the jar, get that using a ZipInputStream and extract the images from there.

Jens Schauder
(Using ZipInputStream rather than ZipFile.)
Tom Hawtin - tackline
+2  A: 

Do you include the applet to a HTML page? If so, try to enable the JAR caching, as is described here: http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/applet_caching.html

If that does not help for some reason :) perhaps expose your resources / images along your applet JAR on a web server and reach them using separate HTTP requests (yes, its ugly and yes, it does not reduce number of needed downloads, but it at least reduces the amount of data that need to be transferred).

david a.
A: 

Which Java VM do you use? And which Server do you use?

  • There is a bug in the browser plugin on Linux.
  • If the server does not send the modified date then Java can not cache the jar file.
Horcrux7
+2  A: 

Simply removing all instances of URLConnection.setDefaultUseCaches(false) will solve the problem.

Please refer for more details.

http://java-junction.blogspot.com/2009/11/applet-jar-caching-not-working.html

Maulin
Very interesting to now...
Laurent K
I didn't set this flag on my code, however this is an interesting point.
michelemarcon