Java Class Load loadClass(String name)

Here you can find the source of loadClass(String name)

Description

Loads the class with the specified name.

License

Apache License

Parameter

Parameter Description
name the name of the class

Return

the resulting Class object

Declaration

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

Method Source Code

//package com.java2s;
/*/*  w ww .ja  va2  s.c o m*/
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

public class Main {
    /**
     * Loads the class with the specified name.  For Java 2 callers, the
     * current thread's context class loader is preferred, falling back on the
     * system class loader of the caller when the current thread's context is not
     * set, or the caller is pre Java 2.
     *
     * @param     name  the name of the class
     * @return    the resulting <code>Class</code> object
     * @exception ClassNotFoundException if the class was not found
     */
    public static Class<?> loadClass(String name) throws ClassNotFoundException {
        return loadClass(name, null);
    }

    /**
     * Loads the class with the specified name.  For Java 2 callers, the
     * current thread's context class loader is preferred, falling back on the
     * class loader of the caller when the current thread's context is not set,
     * or the caller is pre Java 2.  If the callerClassLoader is null, then
     * fall back on the system class loader.
     *
     * @param     name  the name of the class
     * @param     callerClassLoader  the calling class loader context
     * @return    the resulting <code>Class</code> object
     * @exception ClassNotFoundException if the class was not found
     */
    public static Class<?> loadClass(String name, ClassLoader callerClassLoader) throws ClassNotFoundException {
        Class<?> clazz = null;

        try {
            ClassLoader loader = getContextClassLoader();

            if (loader != null)
                clazz = loader.loadClass(name);
        } catch (ClassNotFoundException e) {
            // treat as though loader not set
            ;
        }

        if (clazz == null) {
            if (callerClassLoader != null)
                clazz = callerClassLoader.loadClass(name);
            else
                clazz = Class.forName(name);
        }

        return clazz;
    }

    /**
     * Dynamically accesses the current context class loader.
     * Returns null if there is no per-thread context class loader.
     */
    public static ClassLoader getContextClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }
}

Related

  1. loadClass(String name)
  2. loadClass(String name)
  3. loadClass(String name)
  4. loadClass(String name)
  5. loadClass(String name)
  6. loadClass(String name, ClassLoader cl)
  7. loadClass(String name, ClassLoader ldr)
  8. loadClass(String name, ClassLoader loader)
  9. loadClass(String theClassName, Class theReferrer)