public static Class forName(String name,
                            boolean initialize,
                            ClassLoader loader)
                     throws ClassNotFoundExceptionReturns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier. 
If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void. If name denotes an array class, the component type of the array class is loaded but not initialized. For example, in an instance method the expression:   Class.forName("Foo")
 is equivalent to: 
  Class.forName("Foo", true, this.getClass().getClassLoader())
 Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. 
If the loader is null, and a security manager is present, and the caller's class loader is not null, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the bootstrap class loader.Parameters:
name - fully qualified name of the desired class
initialize - whether the class must be initialized
loader - class loader from which the class must be loaded
Returns:
class object representing the desired class
Throws:
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException - if the class cannot be located by the specified class loader
Since: 
1.2 
See Also: 
forName(String), ClassLoader

解决方案 »

  1.   

    forNamepublic static Class forName(String className)
                         throws ClassNotFoundException
    Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
      Class.forName(className, true, currentLoader)
     
    where currentLoader denotes the defining class loader of the current class. For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:
       Class t = Class.forName("java.lang.Thread")
     
    A call to forName("X") causes the class named X to be initialized.Parameters:className - the fully qualified name of the desired class.Returns:the Class object for the class with the specified name.Throws:LinkageError - if the linkage fails
    ExceptionInInitializerError - if the initialization provoked by this method fails
    ClassNotFoundException - if the class cannot be located
    forNamepublic static Class forName(String name,
                                boolean initialize,
                                ClassLoader loader)
                         throws ClassNotFoundException
    Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter 
    loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier. If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void. If name denotes an array class, the component type of the array class is loaded but not initialized. For example, in an instance method the expression:
      Class.forName("Foo")
     
    is equivalent to:
      Class.forName("Foo", true, this.getClass().getClassLoader())
     
    Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. If the loader is null, and a security manager is present, and the caller's class loader is not null, then this method calls the security manager's checkPermission method with a 
    RuntimePermission("getClassLoader") permission to ensure it's ok to access the bootstrap class loader.Parameters:name - fully qualified name of the desired class
    initialize - whether the class must be initialized
    loader - class loader from which the class must be loadedReturns:class object representing the desired classThrows:LinkageError - if the linkage fails
    ExceptionInInitializerError - if the initialization provoked by this method fails
    ClassNotFoundException - if the class cannot be located by the specified class loaderSince: 1.2See Also: forName(String), ClassLoader
      

  2.   

    public Method getMethod(String name,
                            Class[] parameterTypes)
                     throws NoSuchMethodException,
                            SecurityExceptionReturns a Method object that reflects the specified public member method of the class or interface represented by this Class object. The name parameter is a String specifying the simple name the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes is null, it is treated as if it were an empty array. 
    If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException. If the name is "<init>"or "<clinit>" a NoSuchMethodException is raised. Otherwise, the method to be reflected is determined by the algorithm that follows. Let C be the class represented by this object: C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C. 
    If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected. 
    To find a matching method in a class C:  If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily. 
    See The Java Language Specification, sections 8.2 and 8.4.Parameters:
    name - the name of the method
    parameterTypes - the list of parameters
    Returns:
    the Method object that matches the specified name and parameterTypes
    Throws:
    NoSuchMethodException - if a matching method is not found or if then name is "<init>"or "<clinit>".
    SecurityException - if access to the information is denied.
    Since: 
    JDK1.1 
    See Also: 
    Method, SecurityManager.checkMemberAccess(Class, int), SecurityManager.checkPackageAccess(String)
      

  3.   

    getMethodpublic Method getMethod(String name,
                            Class[] parameterTypes)
                     throws NoSuchMethodException,
                            SecurityException
    Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. The name parameter is a String specifying the simple name the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes is null, it is treated as if it were an empty array. 
    If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException. If the name is "<init>"or "<clinit>" a NoSuchMethodException is raised. Otherwise, the method to be reflected is determined by the algorithm that follows. Let C be the class represented by this object:1. C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C.
    2. If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected.
    To find a matching method in a class C:  If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily. See The Java Language Specification, sections 8.2 and 8.4.Parameters:name - the name of the method
    parameterTypes - the list of parametersReturns:the Method object that matches the specified name and parameterTypesThrows:NoSuchMethodException - if a matching method is not found or if then name is "<init>"or "<clinit>".
    SecurityException - if access to the information is denied.Since: JDK1.1See Also: Method, SecurityManager.checkMemberAccess(Class, int), SecurityManager.checkPackageAccess(String)
      

  4.   

    两位DX:
    这几段jb帮助里的文档我早已经看过,就是由于看过以后还不是很理解,所以才来学问大家的,所以我希望大家能比较具体的说明一下,比如:classloader到底是怎么工作的....谢谢了
      

  5.   

    呵呵,正在敬佩ing,经楼主一说,原来是这么回事