Load a class given its name.
/* * 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. */ import java.net.MalformedURLException; import java.net.URL; /** * A collection of class management utility methods. * * @version $Id: ClassUtils.java 587751 2007-10-24 02:41:36Z vgritsenko $ */ public class ClassUtils { /** * Load a class given its name. * BL: We wan't to use a known ClassLoader--hopefully the heirarchy * is set correctly. * * @param className A class name * @return The class pointed to by <code>className</code> * @exception ClassNotFoundException If a loading error occurs */ public static Class loadClass(String className) throws ClassNotFoundException { return getClassLoader().loadClass(className); } /** * Return a resource URL. * BL: if this is command line operation, the classloading issues * are more sane. During servlet execution, we explicitly set * the ClassLoader. * * @return The context classloader. * @exception MalformedURLException If a loading error occurs */ public static URL getResource(String resource) throws MalformedURLException { return getClassLoader().getResource(resource); } /** * Return the context classloader. * BL: if this is command line operation, the classloading issues * are more sane. During servlet execution, we explicitly set * the ClassLoader. * * @return The context classloader. */ public static ClassLoader getClassLoader() { return Thread.currentThread().getContextClassLoader(); } }