Package java.net
Class URLClassLoader
java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
public class URLClassLoader extends SecureClassLoader
This class loader is responsible for loading classes and resources from a
list of URLs which can refer to either directories or JAR files. Classes
loaded by this
URLClassLoader are granted permission to access the
URLs contained in the URL search list.-
Constructor Summary
Constructors Constructor Description URLClassLoader(URL[] urls)Constructs a newURLClassLoaderinstance.URLClassLoader(URL[] urls, ClassLoader parent)Constructs a new URLClassLoader instance.URLClassLoader(URL[] searchUrls, ClassLoader parent, URLStreamHandlerFactory factory)Constructs a newURLClassLoaderinstance. -
Method Summary
Modifier and Type Method Description protected voidaddURL(URL url)Adds the specified URL to the search list.protected PackagedefinePackage(String packageName, Manifest manifest, URL url)Defines a new package using the information extracted from the specified manifest.protected Class<?>findClass(String className)Tries to locate and load the specified class using the known URLs.URLfindResource(String name)Returns an URL referencing the specified resource ornullif the resource could not be found.Enumeration<URL>findResources(String name)Returns all known URLs which point to the specified resource.protected PermissionCollectiongetPermissions(CodeSource codesource)Gets all permissions for the specifiedcodesource.URL[]getURLs()Returns the search list of thisURLClassLoader.static URLClassLoadernewInstance(URL[] urls)Returns a newURLClassLoaderinstance for the given URLs and the systemClassLoaderas its parent.static URLClassLoadernewInstance(URL[] urls, ClassLoader parentCl)Returns a newURLClassLoaderinstance for the given URLs and the specifiedClassLoaderas its parent.Methods inherited from class java.security.SecureClassLoader
defineClass, defineClassMethods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Constructor Details
-
URLClassLoader
Constructs a newURLClassLoaderinstance. The newly created instance will have the system ClassLoader as its parent. URLs that end with "/" are assumed to be directories, otherwise they are assumed to be JAR files.- Parameters:
urls- the list of URLs where a specific class or file could be found.
-
URLClassLoader
Constructs a new URLClassLoader instance. The newly created instance will have the system ClassLoader as its parent. URLs that end with "/" are assumed to be directories, otherwise they are assumed to be JAR files.- Parameters:
urls- the list of URLs where a specific class or file could be found.parent- the class loader to assign as this loader's parent.
-
URLClassLoader
Constructs a newURLClassLoaderinstance. The newly created instance will have the specifiedClassLoaderas its parent and use the specified factory to create stream handlers. URLs that end with "/" are assumed to be directories, otherwise they are assumed to be JAR files.- Parameters:
searchUrls- the list of URLs where a specific class or file could be found.parent- theClassLoaderto assign as this loader's parent.factory- the factory that will be used to create protocol-specific stream handlers.
-
-
Method Details
-
addURL
Adds the specified URL to the search list.- Parameters:
url- the URL which is to add.
-
findResources
Returns all known URLs which point to the specified resource.- Overrides:
findResourcesin classClassLoader- Parameters:
name- the name of the requested resource.- Returns:
- the enumeration of URLs which point to the specified resource.
- Throws:
IOException- if an I/O error occurs while attempting to connect.
-
getPermissions
Gets all permissions for the specifiedcodesource. First, this method retrieves the permissions from the system policy. If the protocol is "file:/" then a new permission,FilePermission, granting the read permission to the file is added to the permission collection. Otherwise, connecting to and accepting connections from the URL is granted.- Overrides:
getPermissionsin classSecureClassLoader- Parameters:
codesource- the code source object whose permissions have to be known.- Returns:
- the list of permissions according to the code source object.
-
getURLs
Returns the search list of thisURLClassLoader.- Returns:
- the list of all known URLs of this instance.
-
newInstance
Returns a newURLClassLoaderinstance for the given URLs and the systemClassLoaderas its parent.- Parameters:
urls- the list of URLs that is passed to the newURLClassLoader.- Returns:
- the created
URLClassLoaderinstance.
-
newInstance
Returns a newURLClassLoaderinstance for the given URLs and the specifiedClassLoaderas its parent.- Parameters:
urls- the list of URLs that is passed to the new URLClassLoader.parentCl- the parent class loader that is passed to the new URLClassLoader.- Returns:
- the created
URLClassLoaderinstance.
-
findClass
Tries to locate and load the specified class using the known URLs. If the class could be found, a class object representing the loaded class will be returned.- Overrides:
findClassin classClassLoader- Parameters:
className- the name of the class to look for.- Returns:
- the
Classobject that is found. - Throws:
ClassNotFoundException- if the specified class cannot be loaded.
-
findResource
Returns an URL referencing the specified resource ornullif the resource could not be found.- Overrides:
findResourcein classClassLoader- Parameters:
name- the name of the requested resource.- Returns:
- the URL which points to the given resource.
-
definePackage
protected Package definePackage(String packageName, Manifest manifest, URL url) throws IllegalArgumentExceptionDefines a new package using the information extracted from the specified manifest.- Parameters:
packageName- the name of the new package.manifest- the manifest containing additional information for the new package.url- the URL to the code source for the new package.- Returns:
- the created package.
- Throws:
IllegalArgumentException- if a package with the given name already exists.
-