English 中文(简体)
Entire jar is read for every getResourceAsStream
原标题:

I am profiling a java applet startup time. I have noticed that a huge jar of size 5MB is fully read every time a resource from the jar is requested. E.g. images log configuration files, I18N files etc.... Should it behave that way? Is it possible just to jump to the correct point ? Is it possible to read the file only once? If I will switch to cache_archiv_ex and add a preload directive will it help? Example of where the jar file is opened:

java.io.FileInputStream.open(String)
java.io.FileInputStream.<init>(File)
com.sun.deploy.cache.DeployCacheHandler$2.run()
java.security.AccessController.doPrivileged(PrivilegedExceptionAction)
com.sun.deploy.cache.DeployCacheHandler.get(URI, String, Map)
sun.net.www.protocol.http.HttpURLConnection.plainConnect()
sun.net.www.protocol.http.HttpURLConnection.connect()
sun.net.www.protocol.http.HttpURLConnection.getInputStream()
sun.plugin.PluginURLJarFileCallBack.downloadJAR(URLConnection, boolean)
sun.plugin.PluginURLJarFileCallBack.access$000(PluginURLJarFileCallBack, URLConnection, boolean)
sun.plugin.PluginURLJarFileCallBack$2.run()
java.security.AccessController.doPrivileged(PrivilegedExceptionAction)
sun.plugin.PluginURLJarFileCallBack.retrieve(URL)
sun.net.www.protocol.jar.URLJarFile.retrieve(URL, URLJarFile$URLJarFileCloseController)
sun.net.www.protocol.jar.URLJarFile.getJarFile(URL, URLJarFile$URLJarFileCloseController)
sun.net.www.protocol.jar.JarFileFactory.get(URL, boolean)
sun.net.www.protocol.jar.JarURLConnection.connect()
sun.plugin.net.protocol.jar.CachedJarURLConnection.connect()
sun.plugin.net.protocol.jar.CachedJarURLConnection.getInputStream()
java.net.URL.openStream()
java.lang.ClassLoader.getResourceAsStream(String)
sun.plugin2.applet.Applet2ClassLoader.getResourceAsStream(String)
..
.. (real application code stack...)
..
..
java.awt.event.InvocationEvent.dispatch()
java.awt.EventQueue.dispatchEvent(AWTEvent)
java.awt.EventDispatchThread.pumpOneEventForFilters(int)
java.awt.EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter)
java.awt.EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component)
java.awt.EventDispatchThread.pumpEvents(int, Conditional)
java.awt.EventDispatchThread.pumpEvents(Conditional)
java.awt.EventDispatchThread.run()
问题回答

getResourceAsStream is calling CachedJarURLConnection. Are you sure it s not caching the jar and only downloading it once? If it is, I would make sure your web server isn t reporting a different or incorrect last update date on the jar each time.

you can also try to create an index to the jar file with

jar -i "jar file"

And check if you still get that amount of data transfered all the time.

Do you need an applet? Unless you need to embed the Java application within a page, then I suggest using Webstart.

This is a bit late, but maybe someone is still searching for an answer to this (as I did).

I found in this forum post that the problem could be caused by calling to a method called setDefaultUsecaches when creating a new URLConnection:

URL url = new URL( urlString );  
URLConnection connection = url.openConnection();  
connection.setDoInput( true );  
connection.setDoOutput( true );  
connection.setDefaultUseCaches( false ); // <-- This is the problematic line

By removing this line, all of my jars were properly cached and the application start has become lightning fast.

Note: There is another method called setUseCaches in case you need it.

Hope this helps. Best Regards.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签