Class ClassLoaderHelper


  • @Immutable
    public final class ClassLoaderHelper
    extends Object
    ClassLoader utility methods.
    Author:
    Philip Helger
    • Method Detail

      • setContextClassLoader

        public static void setContextClassLoader​(@Nonnull
                                                 ClassLoader aClassLoader)
      • getResource

        @Nullable
        public static URL getResource​(@Nonnull
                                      ClassLoader aClassLoader,
                                      @Nonnull @Nonempty
                                      String sPath)
        Get the URL of the passed resource using the specified class loader only. This is a sanity wrapper around classLoader.getResource (sPath).
        Parameters:
        aClassLoader - The class loader to be used. May not be null.
        sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does NOT start with a slash.
        Returns:
        null if the path could not be resolved using the specified class loader.
      • getResources

        @Nonnull
        public static Enumeration<URL> getResources​(@Nonnull
                                                    ClassLoader aClassLoader,
                                                    @Nonnull @Nonempty
                                                    String sPath)
                                             throws IOException
        Get all URLs of the passed resource using the specified class loader only. This is a sanity wrapper around classLoader.getResources (sPath).
        Parameters:
        aClassLoader - The class loader to be used. May not be null.
        sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does NOT start with a slash.
        Returns:
        null if the path could not be resolved using the specified class loader.
        Throws:
        IOException - In case an internal error occurs.
      • getResourceAsStream

        @Nullable
        public static InputStream getResourceAsStream​(@Nonnull
                                                      ClassLoader aClassLoader,
                                                      @Nonnull @Nonempty
                                                      String sPath)
        Get the input stream of the passed resource using the specified class loader only. This is a sanity wrapper around classLoader.getResourceAsStream (sPath).
        Parameters:
        aClassLoader - The class loader to be used. May not be null.
        sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does NOT start with a slash.
        Returns:
        null if the path could not be resolved using the specified class loader.