Java Utililty Methods Class forName

List of utility methods to do Class forName

Description

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

Method

ClassforName(String className)
Returns the Class object associated with the class or interface with the given string name.
try {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    return loader.loadClass(className);
} catch (ClassNotFoundException e) {
    try {
        return Class.forName(className);
    } catch (ClassNotFoundException ignore) {
    throw e;
ClassforName(String className)
for Name
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
    return Class.forName(className, true, loader);
} catch (ClassNotFoundException e) {
    throw new IllegalStateException("Failed to find class: " + className + " loader=" + loader, e);
ClassforName(String className)
for Name
try {
    return Class.forName(className);
} catch (Exception e) {
    return null;
ClassforName(String className)
for Name
try {
    return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
} catch (SecurityException e) {
return Class.forName(className);
ClassforName(String className, Class instanceOfClass)
for Name
try {
    Class<?> loadedClass = Class.forName(className);
    if (!instanceOfClass.isAssignableFrom(loadedClass)) {
        throw new IllegalStateException(
                "Class " + className + " does not implement " + instanceOfClass.getName());
    return (Class<T>) loadedClass;
} catch (ClassNotFoundException e) {
...
ClassforName(String className, Class superType)
for Name
try {
    Class<?> cls = Thread.currentThread().getContextClassLoader().loadClass(className);
    if (superType.isAssignableFrom(cls)) {
        return (Class<? extends T>) cls;
    } else {
        throw new IllegalStateException(className + " is not " + superType);
} catch (ClassNotFoundException e) {
...
ClassforName(String className, Class xface)
Returns the Class object object associated with the class or interface with the given string name, which is a subclass of xface .
Class<?> theCls;
try {
    theCls = Class.forName(className);
} catch (ClassNotFoundException cnfe) {
    throw new RuntimeException(cnfe);
if (!xface.isAssignableFrom(theCls)) {
    throw new RuntimeException(className + " not " + xface.getName());
...
ClassforName(String className, ClassLoader classLoader)
for Name
if (classLoader == null) {
    return Class.forName(className);
return classLoader.loadClass(className);
ClassforName(String className, ClassLoader defaultClassLoader)
for Name
return defaultClassLoader.loadClass(className);
ClassforName(String name)
for Name
return forName(name, null);