Class JarFileLoader

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public final class JarFileLoader
    extends URLClassLoader
    This class extends URLClassLoader, providing the ability to load a class from a Java jar file introduced after JVM starts. This capability is needed by long running Java applications which cannot be restarted but need to access a new jar file dropped into a well-known location. That jar file path may be added to the JarFileLoader via addJar(java.lang.String) and then packages() used to determine the newly available packages.
    Author:
    Charles Rapp
    • Field Detail

      • URL_FORMAT

        public static final String URL_FORMAT
        Place the JAR file path within this string to create the JAR URL. The URL format is "jar:file:///<path>!/".
        See Also:
        Constant Field Values
    • Constructor Detail

      • JarFileLoader

        public JarFileLoader​(URL[] urls)
        Constructs a new JAR class loader for the specified URLs and the default ClassLoader. The URLs are searched from index zero to urls.length - 1 for classes and resources after first delegating the search to parent. Any URL ending in "/" is assumed to refer to a directory. Otherwise, the URL is assumed to be a JAR file which will be downloaded and opened as necessary.
        Parameters:
        urls - the class and resource load path.
        Throws:
        SecurityException - if a security manager exists and its checkCreateClassLoader method does not allow creation of a class loader.
      • JarFileLoader

        public JarFileLoader​(URL[] urls,
                             ClassLoader parent)
        Constructs a new JAR class loader for the specified URLs and parent class loader. The URLs are searched from index zero to urls.length - 1 for classes and resources after first delegating the search to parent. Any URL ending in "/" is assumed to refer to a directory. Otherwise, the URL is assumed to be a JAR file which will be downloaded and opened as necessary.
        Parameters:
        urls - the class and resource load path.
        parent - the delegate parent class loader.
        Throws:
        SecurityException - if a security manager exists and its checkCreateClassLoader method does not allow creation of a class loader.
      • JarFileLoader

        public JarFileLoader​(URL[] urls,
                             ClassLoader parent,
                             URLStreamHandlerFactory factory)
        Constructs a new JAR class loader for the specified URLs, parent class loader, and URL stream handler factory. The parent argument is the delegate class loader. The factory argument is used to obtain protocol handlers when creating new JAR URLs. The search order is:
        1. parent delegate first.
        2. urls from index zero to urls.length - 1.
        Parameters:
        urls - the class and resource load path.
        parent - the delegate parent class loader.
        factory - the protocol handler factory.
        Throws:
        SecurityException - if a security manager exists and its checkCreateClassLoader method does not allow creation of a class loader.
    • Method Detail

      • packages

        public Package[] packages()
        Returns all the Packages defined by this class loader and its ancestors.
        Returns:
        the class loader packages.
      • addJar

        public void addJar​(String path)
                    throws IOException
        Appends the JAR file path to the URL search list.
        Parameters:
        path - path to a readable JAR file.
        Throws:
        FileNotFoundException - if path refers to a non-existent file.
        IOException - if path either does not refer to a regular file or the file cannot be read.
        MalformedURLException - if path could not be parsed by URL.