Java Class Load loadClass(final String packageName, final String className)

Here you can find the source of loadClass(final String packageName, final String className)

Description

Rather than using the Class.forName mechanism first, this uses Thread.getContextClassLoader instead.

License

Open Source License

Parameter

Parameter Description
packageName the package containing the class
className the class name

Exception

Parameter Description
ClassNotFoundException if class is not accessible from any classloader

Return

the class

Declaration

public static Class<?> loadClass(final String packageName, final String className)
        throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 LegSem./*  w w  w.j  av  a  2  s.  c o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Rather than using the Class.forName mechanism first, this uses
     * Thread.getContextClassLoader instead. In a Servlet context such as
     * Tomcat, this allows JAXB classes for instance to be loaded from the web
     * application (webapp) location while this code might have been loaded from
     * shared/lib. If Thread.getContextClassLoader fails to locate the class
     * then we give a last chance to Class.forName.
     * 
     * @param packageName the package containing the class
     * @param className the class name
     * @return the class
     * @throws ClassNotFoundException if class is not accessible from any class
     *             loader
     */
    public static Class<?> loadClass(final String packageName, final String className)
            throws ClassNotFoundException {
        return loadClass(toQualifiedClassName(packageName, className));
    }

    /**
     * Rather than using the Class.forName mechanism first, this uses
     * Thread.getContextClassLoader instead. In a Servlet context such as
     * Tomcat, this allows JAXB classes for instance to be loaded from the web
     * application (webapp) location while this code might have been loaded from
     * shared/lib. If Thread.getContextClassLoader fails to locate the class
     * then we give a last chance to Class.forName.
     * 
     * @param qualifiedClassName the class name to load
     * @return the class
     * @throws ClassNotFoundException if class is not accessible from any class
     *             loader
     */
    public static Class<?> loadClass(final String qualifiedClassName) throws ClassNotFoundException {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        if (contextClassLoader == null) {
            return Class.forName(qualifiedClassName);
        }
        try {
            return contextClassLoader.loadClass(qualifiedClassName);
        } catch (ClassNotFoundException e) {
            return Class.forName(qualifiedClassName);
        }
    }

    /**
     * Qualifies a class name.
     * 
     * @param packageName the package name, null if none
     * @param className the class name
     * @return a qualified class name
     */
    public static String toQualifiedClassName(final String packageName, final String className) {
        return (packageName == null) ? className : packageName + '.' + className;
    }
}

Related

  1. loadClass(final String className, final ClassLoader classLoader)
  2. loadClass(final String className, final ClassLoader classLoader)
  3. loadClass(final String clazz)
  4. loadClass(final String fqcn)
  5. loadClass(final String lclass)
  6. loadClass(String className)
  7. loadClass(String className)
  8. loadClass(String className)
  9. loadClass(String className)