Java examples for Reflection:Class Loader
Loads a class starting from the given class loader (can be null, then use default class loader)
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { String name = "java2s.com"; System.out.println(forName(name)); }//w ww. j a v a 2 s . c om /** * Loads a class starting from the given class loader (can be null, then use * default class loader) * * @param loader * @param name * of class to load * @return * @throws ClassNotFoundException */ public static Class forName(final ClassLoader loader, final String name) throws ClassNotFoundException { Class klass = null; if (loader != null) { klass = Class.forName(name, false, loader); } else { klass = Class.forName(name, false, ClassLoader.getSystemClassLoader()); } return klass; } /** * Loads a class from the context class loader or, if that fails, from the * default class loader. * * @param name * is the name of the class to load. * @return a <code>Class</code> object. * @throws ClassNotFoundException * if the class was not found. */ public static Class forName(final String name) throws ClassNotFoundException { Class cls = null; try { cls = Class.forName(name, false, Thread.currentThread() .getContextClassLoader()); } catch (Exception e) { cls = Class.forName(name); } return cls; } }