Java Utililty Methods Class Load

List of utility methods to do Class Load

Description

The list of methods to do Class Load are organized into topic(s).

Method

ClassloadClass(String classname, Class clazz)
Tries to load the class from the current thread's context class loader.
ClassLoader loader;
try {
    loader = Thread.currentThread().getContextClassLoader();
    if (loader != null) {
        return Class.forName(classname, false, loader);
} catch (Throwable t) {
if (clazz != null) {
    try {
        loader = clazz.getClassLoader();
        if (loader != null) {
            return Class.forName(classname, false, loader);
    } catch (Throwable t) {
try {
    loader = ClassLoader.getSystemClassLoader();
    if (loader != null) {
        return Class.forName(classname, false, loader);
} catch (Throwable t) {
throw new ClassNotFoundException(classname);
ClassloadClass(String classname, Class clazz)
Tries to load the class from the current thread's context class loader.
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);
ClassloadClass(String classname, Class clazz)
load Class
return getClassLoader(clazz).loadClass(classname);
ClassloadClass(String className, Class context, boolean checkParent)
Loads a class from the classloader; If not found, the classloader of the context class specified will be used.
Class<?> clazz = null;
try {
    clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
    if (context != null) {
        ClassLoader loader = context.getClassLoader();
        while (loader != null) {
            try {
...
ClassloadClass(String className, Class ofType)
Loads and returns the class named className of type superClass.
try {
    Class<?> clazz = Class.forName(className);
    if (ofType == null || !ofType.isAssignableFrom(clazz)) {
        throw new IllegalArgumentException(
                "Class " + className + " must extend or implement " + ofType + "!");
    return (Class<? extends T>) clazz;
} catch (ClassNotFoundException cnfe) {
...
ClassloadClass(String className, Class targetType, ClassLoader cl)
load Class
return loadClass(className, targetType, false, cl);
ClassloadClass(String className, Class type)
Load a class of a particular type.
try {
    ClassLoader loader = type.getClassLoader();
    if (loader == null) {
        loader = Thread.currentThread().getContextClassLoader();
    Class<?> clazz = loader.loadClass(className);
    if (!type.isAssignableFrom(clazz)) {
        throw new RuntimeException("Class [" + clazz + "] is not a [" + type + ']');
...
ClassloadClass(String className, ClassLoader cl)
load Class
if (cl != null) {
    try {
        return cl.loadClass(className);
    } catch (ClassNotFoundException ex) {
return null;
ClassloadClass(String className, ClassLoader classLoader)
load Class
Class<?> objectClass;
try {
    objectClass = Class.forName(className);
} catch (ClassNotFoundException e) {
    if (classLoader != null) {
        objectClass = classLoader.loadClass(className);
    } else {
        throw e;
...
ClassloadClass(String className, ClassLoader classLoader)
load Class
return Class.forName(className, true, classLoader);