Class Loader


  • public final class Loader
    extends Object
    Load resources (or images) from various sources.
    • Method Detail

      • getClassLoader

        public static ClassLoader getClassLoader()
        Returns the ClassLoader to use.
        Returns:
        the ClassLoader.
      • getThreadContextClassLoader

        public static ClassLoader getThreadContextClassLoader()
        Returns the ClassLoader of current thread if possible, or falls back to the system ClassLoader if none is available.
        Returns:
        the TCCL.
        See Also:
        LoaderUtil.getThreadContextClassLoader()
      • getResource

        public static URL getResource​(String resource,
                                      ClassLoader defaultLoader)
        This method will search for resource in different places. The search order is as follows:
        1. Search for resource using the thread context class loader under Java2. If that fails, search for resource using the class loader that loaded this class (Loader). Under JDK 1.1, only the the class loader that loaded this class (Loader) is used.
        2. Try one last time with ClassLoader.getSystemResource(resource), that is is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
        Parameters:
        resource - The resource to load.
        defaultLoader - The default ClassLoader.
        Returns:
        A URL to the resource.
      • getResourceAsStream

        public static InputStream getResourceAsStream​(String resource,
                                                      ClassLoader defaultLoader)
        This method will search for resource in different places. The search order is as follows:
        1. Search for resource using the thread context class loader under Java2. If that fails, search for resource using the class loader that loaded this class (Loader). Under JDK 1.1, only the the class loader that loaded this class (Loader) is used.
        2. Try one last time with ClassLoader.getSystemResource(resource), that is is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
        Parameters:
        resource - The resource to load.
        defaultLoader - The default ClassLoader.
        Returns:
        An InputStream to read the resouce.
      • initializeClass

        public static Class<?> initializeClass​(String className,
                                               ClassLoader loader)
                                        throws ClassNotFoundException
        Loads and initializes a named Class using a given ClassLoader.
        Parameters:
        className - The class name.
        loader - The class loader.
        Returns:
        The class.
        Throws:
        ClassNotFoundException - if the class could not be found.
      • loadClass

        public static Class<?> loadClass​(String className,
                                         ClassLoader loader)
                                  throws ClassNotFoundException
        Loads a named Class using a given ClassLoader.
        Parameters:
        className - The class name.
        loader - The class loader.
        Returns:
        The class, or null if loader is null.
        Throws:
        ClassNotFoundException - if the class could not be found.
      • loadSystemClass

        public static Class<?> loadSystemClass​(String className)
                                        throws ClassNotFoundException
        Load a Class in the java.* namespace by name. Useful for peculiar scenarios typically involving Google App Engine.
        Parameters:
        className - The class name.
        Returns:
        The Class.
        Throws:
        ClassNotFoundException - if the Class could not be found.
      • isClassAvailable

        public static boolean isClassAvailable​(String className)
        Determines if a named Class can be loaded or not.
        Parameters:
        className - The class name.
        Returns:
        true if the class could be found or false otherwise.
      • isJansiAvailable

        public static boolean isJansiAvailable()
      • loadClass

        public static Class<?> loadClass​(String className)
                                  throws ClassNotFoundException
        Loads a class by name. This method respects the #IGNORE_TCCL_PROPERTY Log4j property. If this property is specified and set to anything besides false, then the default ClassLoader will be used.
        Parameters:
        className - The class name.
        Returns:
        the Class for the given name.
        Throws:
        ClassNotFoundException - if the specified class name could not be found