Class ClassLoaderUtils


  • public final class ClassLoaderUtils
    extends java.lang.Object
    Utils associated with "current" class loader. Generally as the current class loader, the thread current classloader is taken. If it does not exist then the class loader of an available class is taken.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.ClassLoader getCurrentClassLoader​(java.lang.Class<?> clazz)
      Returns the "current" class loader.
      static java.net.URL getResource​(java.lang.Class<?> clazz, java.lang.String resource)
      Finds the resource with the given name.
      static java.io.InputStream getResourceAsStream​(java.lang.Class<?> clazz, java.lang.String resource)
      Returns an input stream for reading the specified resource.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getCurrentClassLoader

        public static java.lang.ClassLoader getCurrentClassLoader​(java.lang.Class<?> clazz)
        Returns the "current" class loader. Generally as the current class loader, the thread current classloader is taken. If it does not exist then the class loader of the provided class is taken.
        Parameters:
        clazz - class to get class loader of in case the thread current class loader is not available
        Returns:
        current class loader
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.Class<?> clazz,
                                                              java.lang.String resource)
        Returns an input stream for reading the specified resource.

        This method will search classloader from getCurrentClassLoader(Class) for the resource;

        Parameters:
        clazz - Relative class, which classloader will be used to localize resource
        resource - The resource name
        Returns:
        An input stream for reading the resource, or null if the resource could not be found
      • getResource

        public static java.net.URL getResource​(java.lang.Class<?> clazz,
                                               java.lang.String resource)
        Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

        The name of a resource is a '/'-separated path name that identifies the resource.

        This method will search classloader from getCurrentClassLoader(Class) for the resource

        Parameters:
        clazz - Relative class, which classloader will be used to localize resource
        resource - The resource name
        Returns:
        A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.