Java Class forName forName(String className)

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

Description

Returns the Class object associated with the class or interface with the given string name.

License

Open Source License

Parameter

Parameter Description
className class name to load

Exception

Parameter Description
ClassNotFoundException thrown if class is not found

Return

class

Declaration

public static Class forName(String className) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/*/*from   ww  w.j a  v a  2s  .co m*/
 BillingNG, a next-generation billing solution
 Copyright (C) 2011 Brian Cowdery
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.
 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.html
 */

public class Main {
    /**
     * Returns the <code>Class</code> object associated with the class or
     * interface with the given string name.
     *
     * This method attempts to load the class using the current thread context
     * class loader before the local caller class loader in case the target class
     * has been loaded by a different context (e.g, a web-application).
     *
     * @param className class name to load
     * @return class
     * @throws ClassNotFoundException thrown if class is not found
     */
    public static Class forName(String className) throws ClassNotFoundException {
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            return loader.loadClass(className);

        } catch (ClassNotFoundException e) {
            try {
                return Class.forName(className);
            } catch (ClassNotFoundException ignore) {
                /* ignore and rethrow original */
            }

            throw e;
        }
    }
}

Related

  1. forName(String className)
  2. forName(String className)
  3. forName(String className)
  4. forName(String className)
  5. forName(String className)
  6. forName(String className)
  7. forName(String className)
  8. forName(String className)
  9. forName(String className, Class instanceOfClass)