Java Class Load loadClass(String classname, Class clazz)

Here you can find the source of loadClass(String classname, Class clazz)

Description

Tries to load the class from the current thread's context class loader.

License

LGPL

Parameter

Parameter Description
classname Desired class.
clazz Class object used to obtain a class loader if no context class loader is available.

Return

Class, or null on failure.

Declaration

public static Class loadClass(String classname, Class clazz) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*from ww  w. j a  v  a  2s .co  m*/
     * Tries to load the class from the current thread's context class loader. If
     * not successful, tries to load the class from the current instance.
     * @param classname Desired class.
     * @param clazz Class object used to obtain a class loader
     *             if no context class loader is available.
     * @return Class, or null on failure.
     */
    public static Class loadClass(String classname, Class clazz) throws ClassNotFoundException {
        ClassLoader loader;

        try {
            loader = Thread.currentThread().getContextClassLoader();
            if (loader != null) {
                return loader.loadClass(classname);
            }
        } catch (Throwable t) {
        }

        if (clazz != null) {
            try {
                loader = clazz.getClassLoader();
                if (loader != null) {
                    return loader.loadClass(classname);
                }
            } catch (Throwable t) {
            }
        }

        try {
            loader = ClassLoader.getSystemClassLoader();
            if (loader != null) {
                return loader.loadClass(classname);
            }
        } catch (Throwable t) {
        }

        throw new ClassNotFoundException(classname);
    }
}

Related

  1. loadClass(String className)
  2. loadClass(String className, boolean isInitialized)
  3. loadClass(String className, Class callingClass)
  4. loadClass(String classname, Class clazz)
  5. loadClass(String classname, Class clazz)
  6. loadClass(String className, Class context, boolean checkParent)
  7. loadClass(String className, Class ofType)
  8. loadClass(String className, Class targetType, ClassLoader cl)
  9. loadClass(String className, Class type)