开发者

Java Classloader does not release file handle under Windows

开发者 https://www.devze.com 2023-04-03 18:45 出处:网络
An application needs to load classes from jars that are supplied at runtime. These jars are supposed to be deleted and replaced by other jars, during runtime, which fails because, it would seem, the C

An application needs to load classes from jars that are supplied at runtime. These jars are supposed to be deleted and replaced by other jars, during runtime, which fails because, it would seem, the ClassLoader does not release the file handle aquired on the jars.

Is this a known problem? Is there a classloader available that does not have this problem?

Edit: The use case is not about loadin开发者_如何学JAVAg different versions of the same class, but loading different implementations of the same interface. The classes implement an interface with an "execute" method that the application will have to call. So while an unload mechanism for classes would also be useful in this context, this is not the primary use case.


Look at this What is the reason that setDefaultUseCaches(false) of URLConnection is eagerly called in the org.apache.catalina.core.JreMemoryLeakPreventionListener.

It maybe can answer your question. When using UrlConnection, and set cache true, it will not close the file handler.

Also it gives a workaround to set cache false.

URL url = new URL("jar:file://dummy.jar!/");  
URLConnection uConn = new URLConnection(url) {  
    @Override
    public void connect() throws IOException {          
      // NOOP     
    }  
};
uConn.setDefaultUseCaches(false); 

This just needs to be called in the static block before you use the URLConnection.

It depends on how your classloader is implemented.


The ClassLoader does not release the jar until the class loader has unloaded. You cannot load new versions of classes without creating a new class loader so I don't see how you want this to work.

Perhaps what you want is an OSGi container such as Apache Karaf which allows you to load/unload libraries at runtime.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号