Class JarClassLoader
- java.lang.Object
- java.lang.ClassLoader
- java.security.SecureClassLoader
- com.iizix.util.JarClassLoader
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class JarClassLoader extends java.security.SecureClassLoader implements java.io.Closeable
This class handles one Jar or several Jar file(s) (already loaded), typically XJarFile or MemoryJarFile's.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
Constructors Constructor Description JarClassLoader(IJarFile jar)
Creates the Jar file class loader using the default parent class loader for delegation.JarClassLoader(IJarFile jar, java.lang.ClassLoader parent)
Creates the Jar file class loader with a specified parent class loader for delegation.JarClassLoader(IJarFile jar, java.lang.String subDirectory, java.lang.ClassLoader parent)
Creates the Jar file class loader using the default parent class loader for delegation.JarClassLoader(java.lang.ClassLoader parent, JarFilePath... jarFilePaths)
Creates the Jar file class loader using the default parent class loader for delegation.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(IJarFile jar)
Adds a new jar with root as subdirectory to the class loader.void
add(IJarFile jar, java.lang.String subDirectory)
Adds a new jar with subdirectory to the class loader.void
add(JarFilePath jarFilePath)
Adds a new JarFilePath entry to the class loader.void
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.java.lang.Class<?>
findClass(java.lang.String name)
Finds the class with the specified binary name.java.net.URL
findResource(java.lang.String name)
Finds the resource with the given name.java.util.Enumeration<java.net.URL>
findResources(java.lang.String name)
Returns an enumeration of
objects representing all the resources with the given name.URL
java.io.InputStream
getResourceAsStream(java.lang.String name)
Open for reading, a resource of the specified name from the search path used to load classes.void
initialize()
Initializes the class loader by defining all packages.Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass, getPermissions
Methods 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 Detail
JarClassLoader
public JarClassLoader(IJarFile jar)
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
public JarClassLoader(IJarFile jar, java.lang.ClassLoader parent)
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, java.lang.String subDirectory, java.lang.ClassLoader parent) throws java.lang.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:
java.lang.IllegalArgumentException
- If thesubDirectory
is not found in the Jar file.
JarClassLoader
public JarClassLoader(java.lang.ClassLoader parent, JarFilePath... jarFilePaths) throws java.lang.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:
java.lang.IllegalArgumentException
- If thesubDirectory
is not found in the Jar file.
Method Detail
add
public final void add(IJarFile jar)
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:
java.lang.IllegalStateException
- If class loader already is initialized or has been closed.
add
public final void add(IJarFile jar, java.lang.String subDirectory)
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:
java.lang.IllegalStateException
- If class loader already is initialized or has been closed.
add
public final void add(JarFilePath jarFilePath)
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:
java.lang.IllegalStateException
- If class loader already is initialized or has been closed.
initialize
public final void initialize() throws java.io.IOException
Initializes the class loader by defining all packages.- Throws:
java.io.IOException
- For I/O errors or if there is a sealing problem in the Jar file.java.lang.IllegalStateException
- If class loader already is initialized or has been closed.
close
public void close() throws java.io.IOException
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
close
method 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.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
public final java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException
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:
findClass
in classjava.lang.ClassLoader
- Parameters:
name
- The binary name of the class- Returns:
- The resulting Class object
- Throws:
java.lang.ClassNotFoundException
- If the class could not be found.java.lang.IllegalStateException
- If class loader is not initialized.- See Also:
ClassLoader.findClass(java.lang.String)
findResource
public final java.net.URL findResource(java.lang.String name)
Finds the resource with the given name. Class loader implementations should override this method to specify where to find resources.- Overrides:
findResource
in classjava.lang.ClassLoader
- Parameters:
name
- The resource name.- Returns:
- An
URL
object for reading the resource, ornull
if the resource could not be found. - Throws:
java.lang.IllegalStateException
- If class loader is not initialized.
findResources
public final java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
Returns an enumeration of
objects representing all the resources with the given name. Class loader implementations should override this method to specify where to load resources from.URL
- Overrides:
findResources
in classjava.lang.ClassLoader
- Parameters:
name
- The resource name.- Returns:
- An enumeration of
objects for the resources.URL
- Throws:
java.lang.IllegalStateException
- If class loader is not initialized.
getResourceAsStream
public final java.io.InputStream getResourceAsStream(java.lang.String name)
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:
getResourceAsStream
in classjava.lang.ClassLoader
- 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:
java.lang.IllegalStateException
- If class loader is not initialized.- See Also:
ClassLoader.getResourceAsStream(java.lang.String)