Java Class forName forName(String name)

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

Description

A utility for using the current class loader (rather than the system class loader) when instantiating a new class.

License

Apache License

Parameter

Parameter Description
name class name to load

Return

the newly loaded class

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w w w . j  av  a2  s .  c  om*/
 * #%L
 * P6Spy
 * %%
 * Copyright (C) 2013 P6Spy
 * %%
 * Licensed 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.
 * #L%
 */

public class Main {
    /**
     * A utility for using the current class loader (rather than the
     * system class loader) when instantiating a new class.
     * <p>
     * The idea is that the thread's current loader might have an
     * obscure notion of what your class path is (e.g. an app server) that
     * will not be captured properly by the system class loader.
     * <p>
     * taken from http://sourceforge.net/forum/message.php?msg_id=1720229
     *
     * @param name class name to load
     * @return the newly loaded class
     */
    public static Class<?> forName(String name)
            throws ClassNotFoundException {
        ClassLoader ctxLoader = null;
        try {
            ctxLoader = Thread.currentThread().getContextClassLoader();
            return Class.forName(name, true, ctxLoader);

        } catch (ClassNotFoundException ex) {
            // try to fall through and use the default
            // Class.forName
            //if(ctxLoader == null) { throw ex; }
        } catch (SecurityException ex) {
        }
        return Class.forName(name);
    }
}

Related

  1. forName(String className, ClassLoader defaultClassLoader)
  2. forName(String name)
  3. forName(String name)
  4. forName(String name)
  5. forName(String name)
  6. forName(String name)
  7. forName(String name, Class caller)
  8. forName(String origClassName)
  9. forName(String type, Class cast)