Java URLClassLoader
Closing a URLClassLoader
By Michael McMahon
Complex Java programs, such as application servers, sometimes create their own class loaders using the URLClassLoader type. With URLClassLoader, applications can load classes and resources from a search path of URLs. The following URL types are supported:
file: (loads from file-system directories)
jar: (loads from JAR files)
http: (loads from http servers)
A frequent problem has been how to support updated implementations of the classes and resources loaded from a particular codebase, and in particular from JAR files. In principle, once the application clears all references to a loader object, the garbage collector and finalization mechanisms will eventually ensure that all resources (such as the JarFile objects) are released and closed.
The application can then replace the JAR file, and create a new URLClassLoader instance to load from the same location, but this time using the new implementation of the classes/resources.
However, since it can't be predicted exactly when finalization and garbage collection will occur, this causes problems for applications which need to be able to do this in a predictable and timely fashion. It is a particular problem on Windows, because open files cannot be deleted or replaced.
To alleviate this problem, URLClassLoader has acquired a new method called close(). It has been implemented since Build 48 of JDK7.
The close() method effectively invalidates the loader, so that no new classes can be loaded from it. It also closes any JAR files that were opened by the loader. This allows the application to delete or replace these files and, if necessary, create new loaders using new implementations.
The new method follows the familiar "Closeable" pattern, and URLClassLoader now implements the Closeable interface, which defines URLClassLoader.close(). The following sample code shows how one might use the method.
&n
Ïà¹ØÎĵµ£º
¡¾IT168 ¼¼ÊõÎÄÕ¡¿
WWJ SDK ÊÇÒ»ÖÖ¹¹½¨ÔÚ Java OpenGL (JOGL) À©Õ¹Ö®É쵀 3D
ͼÐεØÇòÒÇ¡£WWJ Àà²ã´Î½á¹¹µÄºËÐÄÊÇ GLCanvas µÄ×ÓÀà WorldWindowGLCanvas¡£¶ø GLCanvas ÊÇÒ»¸ö
Abstract Window Toolkit (AWT) ×é¼þ¡£
¡¡¡¡WWJ ¶Ô AWT µÄÒÀÀµÐÔ¶ÔÓÚÏëÔÚ Eclipse
Ó¦ÓóÌÐòÖÐʹÓà WWJ µÄ GIS ¿ª·¢ÈËÔ±À´½²ÊÇÒ»¸öÕÏ°¡£Äú¿É ......
public class VariableParameter {
public static void main(String[] args) {
System.out.println(add(1,2));
System.out.println(add(1,2,3,4));
}
public static int add(int x,int...args){
//ÊÇ3¸öµã£¬args±ØÐëÊÇ ......
Òë×¢
£º±¾ÎÄÀ´×Ôstraight_talking_java@yahoogroups.comÌÖÂÛ×飬ÒѾÊÇÒ»Äê¶àÇ°
µÄÎÄÕ¡£Alan WilliamsonÊÇJava Developers
JournalµÄ±à¼£¬ÏÂÎÄÀ´×ÔËûÔÚIBMµÄÒ»¸öÏûÏ¢À´Ô´¡£SWTºÍSwingµÄÂÛÕùÎÒ¼û¹ý²»ÉÙ£¬NetbeansºÍEclipseµÄҲͬÑù¶à¡£ÒëÕß·Òë
´ËÎIJ¢²»ÊÇÒª¼¤ÆðʲôÕùÖ´£¬Ò²²»ÊÇÖ§³ÖÄÄÒ»·½£¨ËäÈ»ÎÒµÄÈ·ÊÇÕ¾ÔÚSWTÒ»±ßµ ......
1¡¢ ¶ÔÏóµÄ³õʼ»¯
(1) ·Ç¾²Ì¬¶ÔÏóµÄ³õʼ»¯
ÔÚ´´½¨¶ÔÏóʱ£¬¶ÔÏóËùÔÚÀàµÄËùÓÐÊý¾Ý³ÉÔ±»áÊ×ÏȽøÐгõʼ»¯¡£
»ù±¾ÀàÐÍ£ºintÐÍ£¬³õʼ»¯Îª0¡£
Èç¹ûΪ¶ÔÏó£ºÕâЩ¶ÔÏó»á°´Ë³Ðò³õʼ»¯¡£
¡ùÔÚËùÓÐÀà³ÉÔ±³õʼ»¯Íê³ÉÖ®ºó£¬²Åµ÷Óñ¾ÀàµÄ¹¹Ôì·½·¨´´½¨¶ÔÏó¡£
¹¹Ôì·½·¨µÄ×÷ÓþÍÊdzõʼ»¯¡£
(2) ¾²Ì¬¶ÔÏóµÄ³õʼ»¯
³ÌÐòÖÐÖ÷ÀàµÄ ......