Class JarClassLoader
- All Implemented Interfaces:
Closeable,AutoCloseable
Please note that this class is NOT thread safe for adding additional Jars and subdirectories. This should be done PRIOR to using the class loader. This way, the code does not use any synchronization and is therefore faster.
- Author:
- Christopher Mindus
Constructor Summary
ConstructorsConstructorDescriptionJarClassLoader(IJarFile jar) Creates the Jar file class loader using the default parent class loader for delegation.JarClassLoader(IJarFile jar, ClassLoader parent) Creates the Jar file class loader with a specified parent class loader for delegation.JarClassLoader(IJarFile jar, String subDirectory, ClassLoader parent) Creates the Jar file class loader using the default parent class loader for delegation.JarClassLoader(ClassLoader parent, JarFilePath... jarFilePaths) Creates the Jar file class loader using the default parent class loader for delegation.Method Summary
Modifier and TypeMethodDescriptionfinal voidAdds a new jar with root as subdirectory to the class loader.final voidAdds a new jar with subdirectory to the class loader.final voidadd(JarFilePath jarFilePath) Adds a new JarFilePath entry to the class loader.voidclose()Closes this class loader, so that it can no longer be used to load new classes or resources that are defined by this loader.final Class<?>Finds the class with the specified binary name.final URLfindResource(String name) Finds the resource with the given name.final Enumeration<URL>findResources(String name) Returns an enumeration ofobjects representing all the resources with the given name.URLfinal InputStreamgetResourceAsStream(String name) Open for reading, a resource of the specified name from the search path used to load classes.final voidInitializes the class loader by defining all packages.Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass, getPermissionsMethods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
Constructor Details
JarClassLoader
Creates the Jar file class loader using the default parent class loader for delegation.It is IMPORTANT to call
before using the class loader.initialize()- Parameters:
jar- The Jar file for the class path.
JarClassLoader
Creates the Jar file class loader with a specified parent class loader for delegation.It is IMPORTANT to call
before using the class loader.initialize()- Parameters:
jar- The Jar file for the class path.parent- The Parent class loader.
JarClassLoader
public JarClassLoader(IJarFile jar, String subDirectory, ClassLoader parent) throws IllegalArgumentException Creates the Jar file class loader using the default parent class loader for delegation.It is IMPORTANT to call
before using the class loader.initialize()- Parameters:
jar- The Jar file for the class path.subDirectory- Path inside the Jar file, e.g. System/classpath/bin/, or null for the entire JarFile. If an entry is not ending with a "/", it will be appended.parent- The Parent class loader.- Throws:
IllegalArgumentException- If thesubDirectoryis not found in the Jar file.
JarClassLoader
public JarClassLoader(ClassLoader parent, JarFilePath... jarFilePaths) throws IllegalArgumentException Creates the Jar file class loader using the default parent class loader for delegation.It is IMPORTANT to call
before using the class loader.initialize()- Parameters:
parent- The Parent class loader.jarFilePaths- Jar file and paths list or array.- Throws:
IllegalArgumentException- If thesubDirectoryis not found in the Jar file.
Method Details
add
Adds a new jar with root as subdirectory to the class loader. Please note that this call must be done prior to using the class loader as it is not thread safe.- Parameters:
jar- The Jar file for the class path.- Throws:
IllegalStateException- If class loader already is initialized or has been closed.
add
Adds a new jar with subdirectory to the class loader. Please note that this call must be done prior to using the class loader as it is not thread safe.- Parameters:
jar- The Jar file for the class path.subDirectory- Path inside the Jar file, e.g. System/classpath/bin/, or null for the entire JarFile. If an entry is not ending with a "/", it will be appended.- Throws:
IllegalStateException- If class loader already is initialized or has been closed.
add
Adds a new JarFilePath entry to the class loader. Please note that this call must be done prior to using the class loader as it is not thread safe.- Parameters:
jarFilePath- The Jar file for the class path.- Throws:
IllegalStateException- If class loader already is initialized or has been closed.
initialize
Initializes the class loader by defining all packages.- Throws:
IOException- For I/O errors or if there is a sealing problem in the Jar file.IllegalStateException- If class loader already is initialized or has been closed.
close
Closes this class loader, so that it can no longer be used to load new classes or resources that are defined by this loader. Classes and resources defined by any of this loader's parents in the delegation hierarchy are still accessible. Also, any classes or resources that are already loaded, are still accessible.In the case of jar:, file:, xjar: and memjar: URLs, it also closes any files that were opened by it. If another thread is loading a class when the
closemethod is invoked, then the result of that load is undefined.The method makes a best effort attempt to close all opened files, by catching
IOException's internally. Unchecked exceptions and errors are not caught. Calling close on an already closed loader has no effect.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- 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.
findClass
Finds the class with the specified binary name. This method is overridden by this class loader implementations that follow the delegation model for loading classes, and will be invoked by the loadClass method after checking the parent class loader for the requested class.- Overrides:
findClassin classClassLoader- Parameters:
name- The binary name of the class- Returns:
- The resulting Class object
- Throws:
ClassNotFoundException- If the class could not be found.IllegalStateException- If class loader is not initialized.- See Also:
findResource
Finds the resource with the given name. Class loader implementations should override this method to specify where to find resources.- Overrides:
findResourcein classClassLoader- Parameters:
name- The resource name.- Returns:
- An
URLobject for reading the resource, ornullif the resource could not be found. - Throws:
IllegalStateException- If class loader is not initialized.
findResources
Returns an enumeration ofobjects representing all the resources with the given name. Class loader implementations should override this method to specify where to load resources from.URL- Overrides:
findResourcesin classClassLoader- Parameters:
name- The resource name.- Returns:
- An enumeration of
objects for the resources.URL - Throws:
IllegalStateException- If class loader is not initialized.
getResourceAsStream
Open for reading, a resource of the specified name from the search path used to load classes. This method locates the resource through the system class loader (see getSystemClassLoader()).- Overrides:
getResourceAsStreamin classClassLoader- Parameters:
name- the name of the resource.- Returns:
- An input stream for reading the resource, or null if the resource could not be found.
- Throws:
IllegalStateException- If class loader is not initialized.- See Also: