Class JarClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable

public class JarClassLoader extends SecureClassLoader implements 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 Details

    • JarClassLoader

      public JarClassLoader(IJarFile jar)
      Creates the Jar file class loader using the default parent class loader for delegation.

      It is IMPORTANT to call initialize() before using the class loader.

      Parameters:
      jar - The Jar file for the class path.
    • JarClassLoader

      public JarClassLoader(IJarFile jar, ClassLoader parent)
      Creates the Jar file class loader with a specified parent class loader for delegation.

      It is IMPORTANT to call initialize() before using the class loader.

      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 initialize() before using the class loader.

      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 the subDirectory is 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 initialize() before using the class loader.

      Parameters:
      parent - The Parent class loader.
      jarFilePaths - Jar file and paths list or array.
      Throws:
      IllegalArgumentException - If the subDirectory is not found in the Jar file.
  • Method Details

    • 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:
      IllegalStateException - If class loader already is initialized or has been closed.
    • add

      public final void add(IJarFile jar, 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:
      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:
      IllegalStateException - If class loader already is initialized or has been closed.
    • initialize

      public final void initialize() throws IOException
      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

      public void close() throws 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 interface AutoCloseable
      Specified by:
      close in interface Closeable
      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

      public final Class<?> findClass(String name) throws 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 class ClassLoader
      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

      public final URL findResource(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 class ClassLoader
      Parameters:
      name - The resource name.
      Returns:
      An URL object for reading the resource, or null if the resource could not be found.
      Throws:
      IllegalStateException - If class loader is not initialized.
    • findResources

      public final Enumeration<URL> findResources(String name)
      Returns an enumeration of URL objects representing all the resources with the given name. Class loader implementations should override this method to specify where to load resources from.
      Overrides:
      findResources in class ClassLoader
      Parameters:
      name - The resource name.
      Returns:
      An enumeration of URL objects for the resources.
      Throws:
      IllegalStateException - If class loader is not initialized.
    • getResourceAsStream

      public final InputStream getResourceAsStream(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 class 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:
      IllegalStateException - If class loader is not initialized.
      See Also: