Example usage for java.lang ClassLoader loadClass

List of usage examples for java.lang ClassLoader loadClass

Introduction

In this page you can find the example usage for java.lang ClassLoader loadClass.

Prototype

public Class<?> loadClass(String name) throws ClassNotFoundException 

Source Link

Document

Loads the class with the specified binary name.

Usage

From source file:Main.java

public static Class loadClass(String clazz, boolean warn) throws ClassNotFoundException {
    try {/* w ww  . java  2 s.com*/
        ClassLoader tcl = getTCL();

        if (tcl != null) {
            Class c = tcl.loadClass(clazz);
            if (c != null) {
                return c;
            }
        }
    } catch (Throwable e) {
    }
    // we reached here because tcl was null or because of a
    // security exception, or because clazz could not be loaded...
    // In any case we now try one more time
    return Class.forName(clazz);
}

From source file:com.codebase.foundation.classloader.xbean.ClassLoaderUtil.java

/**
 * Releases the specified classloader from the Apache Jakarta Commons Logging class loader cache using reflection.
 * @param classLoader the class loader to release
 *///from   w  w w  .  j a v a 2  s  .  c o m
@SuppressWarnings("all")
public static void releaseCommonsLoggingCache(ClassLoader classLoader) {
    try {
        Class logFactory = classLoader.loadClass("org.apache.commons.logging.LogFactory");
        Method release = logFactory.getMethod("release", new Class[] { ClassLoader.class });
        release.invoke(null, new Object[] { classLoader });
    } catch (Throwable ignored) {
        // there is nothing a user could do about this anyway
    }
}

From source file:net.mindengine.blogix.utils.BlogixUtils.java

private static Class<?> findClassInClassLoaders(ClassLoader[] classLoaders, String classPath)
        throws ClassNotFoundException {
    for (ClassLoader classLoader : classLoaders) {
        try {//from   www  . jav a 2 s. co m
            return classLoader.loadClass(classPath);
        } catch (ClassNotFoundException e) {
        }
    }
    throw new ClassNotFoundException(classPath);
}

From source file:Main.java

/**
 * Loads the class with the specified name. This method first attempts  to load the
 * class with the current context classloader and only if the search failed, it
 * tries to load the class with the given class loader.
 *
 * @param name the name of the class.//from  w  ww .j ava 2  s  .  co m
 * @param loader the classloader to load the class.
 *
 * @return the result {@link java.lang.Class} object.
 *
 * @throws ClassNotFoundException if the class was not found.
 */
private static Class loadClass(final String name, final ClassLoader loader) throws ClassNotFoundException {
    ClassLoader l = Thread.currentThread().getContextClassLoader();

    if (l != null) {
        try {
            return l.loadClass(name);
        } catch (ClassNotFoundException ignored) {
            // fall back to the given classloader
        }
    }

    return loader.loadClass(name);
}

From source file:Main.java

/**
 * Loads a class from the classloader; /* w  w  w  . j  a  v  a 2 s  . c  o m*/
 * If not found, the classloader of the {@code context} class specified will be used.
 * If the flag {@code checkParent} is true, the classloader's parent is included in 
 * the lookup.
 */
static Class<?> loadClass(String className, Class<?> context, boolean checkParent) {
    Class<?> clazz = null;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
        if (context != null) {
            ClassLoader loader = context.getClassLoader();
            while (loader != null) {
                try {
                    clazz = loader.loadClass(className);
                    return clazz;
                } catch (ClassNotFoundException e1) {
                    loader = checkParent ? loader.getParent() : null;
                }
            }
        }
    }
    return clazz;
}

From source file:Main.java

/**
 * Check whether the given class is visible in the given ClassLoader.
 *
 * @param clazz       the class to check (typically an interface)
 * @param classLoader the ClassLoader to check against (may be {@code null},
 *                    in which case this method will always return {@code true})
 *///from w ww.j  a  v  a2 s .c  o m
public static boolean isVisible(Class<?> clazz, ClassLoader classLoader) {
    if (classLoader == null) {
        return true;
    }
    try {
        Class<?> actualClass = classLoader.loadClass(clazz.getName());
        return (clazz == actualClass);
        // Else: different interface class found...
    } catch (ClassNotFoundException ex) {
        // No interface class found...
        return false;
    }
}

From source file:ReflectionHelper.java

public static Class<?> loadClass(String name, Class<?> caller) throws ClassNotFoundException {
    try {/*from w  ww  .  j av  a 2  s  .co m*/
        //try context classloader, if fails try caller classloader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader != null) {
            return loader.loadClass(name);
        }
    } catch (ClassNotFoundException e) {
        //trying caller classloader
        if (caller == null) {
            throw e;
        }
    }
    return Class.forName(name, true, caller.getClassLoader());
}

From source file:Main.java

/** Loads the class with the given name, or null if it cannot be loaded. */
public static Class<?> loadClass(final String className, final ClassLoader classLoader) {
    try {//from w  w  w .j  ava  2s.co  m
        if (classLoader == null)
            return Class.forName(className);
        return classLoader.loadClass(className);
    } catch (final ClassNotFoundException e) {
        return null;
    }
}

From source file:com.braffdev.server.core.utilities.HandlerClassListInflater.java

/**
 * @param clazz//  w  ww .  ja v  a2  s . co  m
 * @return
 * @throws ClassNotFoundException
 */
private static Class<?> loadClass(String clazz) throws ClassNotFoundException {
    ClassLoader classLoader = Server.getInstance().getClassLoader();
    return classLoader.loadClass(clazz);
}

From source file:Main.java

/** Checks whether a class with the given name exists. */
public static boolean hasClass(final String className, final ClassLoader classLoader) {
    try {/*  ww  w.jav  a  2 s .  co  m*/
        if (classLoader == null)
            Class.forName(className);
        else
            classLoader.loadClass(className);
        return true;
    } catch (final ClassNotFoundException e) {
        return false;
    }
}