Tutorial :JAR multiple download



Question:

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?


Solution:1

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).


Solution:2

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


Solution:3

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.


Solution:4

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.


Solution:5

If your applet always downloads the jar even though jar is cached, make sure you have not disabled the URLConnection's caching via the API: URLConnection.setUseCaches and URLConnection.SetDefaultUseCaches.

ImageIcon's underlying mechanism for fetching the resource is a URLConnection. Calling URLConnection.setDefaultUseCaches(false), sets a "part of the static state of all URLConnections" which cause the JRE to ignore the cache and redownload the entire jar every time it accessed.

Simply removing all instances of setDefaultUseCaches will solve the problem.

this is a repost from: http://java-junction.blogspot.com/2009/11/applet-jar-caching-not-working.html


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »