public class URLClassLoader 
   extends SecureClassLoader implements Closeable
| java.lang.Object | |||
| ↳ | java.lang.ClassLoader | ||
| ↳ | java.security.SecureClassLoader | ||
| ↳ | java.net.URLClassLoader | ||
这个类加载器用于从指向JAR文件和目录的URL的搜索路径加载类和资源。 任何以'/'结尾的URL都被假定为指向一个目录。 否则,假定该URL是指将根据需要打开的JAR文件。
随后加载类和资源时,将使用创建URLClassLoader实例的线程的AccessControlContext。
默认情况下,加载的类仅授予访问创建URLClassLoader时指定的URL的权限。
| Public constructors | |
|---|---|
|  URLClassLoader(URL[] urls, ClassLoader parent) 为给定的URL构造一个新的URLClassLoader。 | |
|  URLClassLoader(URL[] urls) 使用默认委派父级  | |
|  URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) 为指定的URL,父类加载器和URLStreamHandlerFactory构造一个新的URLClassLoader。 | |
| Public methods | |
|---|---|
|  void |  close() 关闭此URLClassLoader,以便它不能再用于加载由此加载器定义的新类或资源。 | 
|  URL |  findResource(String name) 在URL搜索路径中查找具有指定名称的资源。 | 
|  Enumeration<URL> |  findResources(String name) 返回表示具有指定名称的URL搜索路径上所有资源的URL的枚举。 | 
|  InputStream |  getResourceAsStream(String name) 返回读取指定资源的输入流。 | 
|  URL[] |  getURLs() 返回加载类和资源的URL的搜索路径。 | 
|  static URLClassLoader |  newInstance(URL[] urls, ClassLoader parent) 为指定的URL和父类加载器创建一个新的URLClassLoader实例。 | 
|  static URLClassLoader |  newInstance(URL[] urls) 为指定的URL和默认的父类加载器创建一个新的URLClassLoader实例。 | 
| Protected methods | |
|---|---|
|  void |  addURL(URL url) 将指定的URL追加到URL列表中以搜索类和资源。 | 
|  软件包 |  definePackage(String name, Manifest man, URL url) 在此ClassLoader中按名称定义一个新包。 | 
|  Class<?> |  findClass(String name) 从URL搜索路径中查找并加载具有指定名称的类。 | 
|  PermissionCollection |  getPermissions(CodeSource codesource) 返回给定codesource对象的权限。 | 
| Inherited methods | |
|---|---|
|  From class  java.security.SecureClassLoader  | |
|  From class  java.lang.ClassLoader  | |
|  From class  java.lang.Object  | |
|  From interface  java.io.Closeable  | |
|  From interface  java.lang.AutoCloseable  | |
URLClassLoader (URL[] urls, ClassLoader parent)
为给定的URL构造一个新的URLClassLoader。 在首次搜索指定的父类加载器后,将按照为类和资源指定的顺序来搜索URL。 任何以'/'结尾的URL都被假定为指向一个目录。 否则,假定该URL是指将根据需要下载和打开的JAR文件。
如果存在安全管理器,则此方法首先调用安全管理器的方法 checkCreateClassLoader以确保允许创建类加载器。
| Parameters | |
|---|---|
| urls | URL: the URLs from which to load classes and resources | 
| parent | ClassLoader: the parent class loader for delegation | 
| Throws | |
|---|---|
| SecurityException | if a security manager exists and its checkCreateClassLoadermethod doesn't allow creation of a class loader. | 
也可以看看:
URLClassLoader (URL[] urls)
使用默认委派父级ClassLoader为指定的URL构造一个新的URLClassLoader。 在首次在父类加载器中进行搜索之后,将按照为类和资源指定的顺序来搜索URL。 任何以'/'结尾的URL都被假定为指向一个目录。 否则,假定该URL是指将根据需要下载和打开的JAR文件。
如果有安全管理器,则此方法首先调用安全管理器的方法 checkCreateClassLoader以确保允许创建类加载器。
| Parameters | |
|---|---|
| urls | URL: the URLs from which to load classes and resources | 
| Throws | |
|---|---|
| SecurityException | if a security manager exists and its checkCreateClassLoadermethod doesn't allow creation of a class loader. | 
也可以看看:
URLClassLoader (URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)
为指定的URL,父类加载器和URLStreamHandlerFactory构造一个新的URLClassLoader。 父参数将用作委派的父类加载器。 factory参数将用作流处理程序工厂,以在创建新的jar网址时获取协议处理程序。
如果有安全管理器,则此方法首先调用安全管理器的方法 checkCreateClassLoader以确保允许创建类加载器。
| Parameters | |
|---|---|
| urls | URL: the URLs from which to load classes and resources | 
| parent | ClassLoader: the parent class loader for delegation | 
| factory | URLStreamHandlerFactory: the URLStreamHandlerFactory to use when creating URLs | 
| Throws | |
|---|---|
| SecurityException | if a security manager exists and its checkCreateClassLoadermethod doesn't allow creation of a class loader. | 
也可以看看:
void close ()
关闭此URLClassLoader,以便它不能再用于加载由此加载器定义的新类或资源。 该装载程序的父级在代理层次结构中定义的类和资源仍可访问。 此外,任何已经加载的类或资源仍然可以访问。
在jar:和file:URL的情况下,它也关闭所有由它打开的文件。 如果另一个线程在调用close方法时正在加载类,那么该加载的结果是未定义的。
该方法尽最大努力尝试关闭所有打开的文件,在内部捕获IOException 。 未检查的异常和错误未被捕获。 在已经关闭的加载器上调用close没有效果。
| Throws | |
|---|---|
| IOException | if closing any file opened by this class loader resulted in an IOException. Any such exceptions are caught internally. If only one is caught, then it is re-thrown. If more than one exception is caught, then the second and following exceptions are added as suppressed exceptions of the first one caught, which is then re-thrown. | 
| SecurityException | if a security manager is set, and it denies RuntimePermission("closeClassLoader") | 
URL findResource (String name)
在URL搜索路径中查找具有指定名称的资源。
| Parameters | |
|---|---|
| name | String: the name of the resource | 
| Returns | |
|---|---|
| URL | a URLfor the resource, ornullif the resource could not be found, or if the loader is closed. | 
Enumeration<URL> findResources (String name)
返回表示具有指定名称的URL搜索路径上所有资源的URL的枚举。
| Parameters | |
|---|---|
| name | String: the resource name | 
| Returns | |
|---|---|
| Enumeration<URL> | an EnumerationofURLs If the loader is closed, the Enumeration will be empty. | 
| Throws | |
|---|---|
| IOException | if an I/O exception occurs | 
InputStream getResourceAsStream (String name)
返回读取指定资源的输入流。 如果此加载器已关闭,则此方法打开的任何资源将被关闭。
搜索顺序在 getResource(String)的文档中有描述。
| Parameters | |
|---|---|
| name | String: The resource name | 
| Returns | |
|---|---|
| InputStream | An input stream for reading the resource, or null if the resource could not be found | 
URL[] getURLs ()
返回加载类和资源的URL的搜索路径。 这包括为构造函数指定的原始URL列表,以及随后由addURL()方法附加的URL。
| Returns | |
|---|---|
| URL[] | the search path of URLs for loading classes and resources. | 
URLClassLoader newInstance (URL[] urls, ClassLoader parent)
为指定的URL和父类加载器创建一个新的URLClassLoader实例。 如果安装了安全管理器,该loadClass的URLClassLoader的方法此方法返回的将调用SecurityManager.checkPackageAccess加载类方法之前。
| Parameters | |
|---|---|
| urls | URL: the URLs to search for classes and resources | 
| parent | ClassLoader: the parent class loader for delegation | 
| Returns | |
|---|---|
| URLClassLoader | the resulting class loader | 
URLClassLoader newInstance (URL[] urls)
为指定的URL和默认的父类加载器创建一个新的URLClassLoader实例。 如果安装了安全管理器,该loadClass的URLClassLoader的方法此方法返回的将调用SecurityManager.checkPackageAccess加载类之前。
| Parameters | |
|---|---|
| urls | URL: the URLs to search for classes and resources | 
| Returns | |
|---|---|
| URLClassLoader | the resulting class loader | 
void addURL (URL url)
将指定的URL追加到URL列表中以搜索类和资源。
如果指定的URL是 null或者已经在URL列表中,或者该加载器已关闭,则调用此方法不起作用。
| Parameters | |
|---|---|
| url | URL: the URL to be added to the search path of URLs | 
软件包 definePackage (String name, Manifest man, URL url)
在此ClassLoader中按名称定义一个新包。 包含在指定清单中的属性将用于获取包装版本和密封信息。 对于密封的包,附加的URL指定从中加载包的代码源URL。
| Parameters | |
|---|---|
| name | String: the package name | 
| man | Manifest: the Manifest containing package version and sealing information | 
| url | URL: the code source url for the package, or null if none | 
| Returns | |
|---|---|
| 软件包 | the newly defined Package object | 
| Throws | |
|---|---|
| IllegalArgumentException | if the package name duplicates an existing package either in this class loader or one of its ancestors | 
Class<?> findClass (String name)
从URL搜索路径中查找并加载具有指定名称的类。 任何引用JAR文件的URL都会根据需要加载和打开,直到找到类。
| Parameters | |
|---|---|
| name | String: the name of the class | 
| Returns | |
|---|---|
| Class<?> | the resulting class | 
| Throws | |
|---|---|
| ClassNotFoundException | if the class could not be found, or if the loader is closed. | 
PermissionCollection getPermissions (CodeSource codesource)
返回给定codesource对象的权限。 此方法的实现首先调用super.getPermissions,然后根据codesource的URL添加权限。
如果此URL的协议是“jar”,则授予的权限将基于Jar文件的URL所需的权限。
如果协议是“文件”并且存在授权组件,则可以授予连接并接受来自该授权的连接的权限。 如果协议是“文件”并且路径指定了文件,则授予读取该文件的权限。 如果协议是“文件”,路径是目录,则会授予权限以读取所有文件和(递归)读取该目录中包含的所有文件和子目录。
如果协议不是“文件”,则允许连接并接受来自URL主机的连接。
| Parameters | |
|---|---|
| codesource | CodeSource: the codesource | 
| Returns | |
|---|---|
| PermissionCollection | the permissions granted to the codesource |