当前位置: 代码迷 >> Java相关 >> 怎么发生错误了 这里Class c1=Class.forName(name);
  详细解决方案

怎么发生错误了 这里Class c1=Class.forName(name);

热度:322   发布时间:2010-11-06 08:48:57.0
怎么发生错误了 这里Class c1=Class.forName(name);
import java.util.*;
import java.lang.reflect.*;
public class ReflectionTest {
   
    public static void  printConstructors(Class c1)
    {
        Constructor[] construtors=c1.getDeclaredConstructors();
        
        for(Constructor c:construtors)
        {
            String name=c.getName();
            System.out.print("  "+Modifier.toString(c.getModifiers()));
            System.out.print(" "+name+" (");
            
            Class[] paramTypes =c.getParameterTypes();
            for(int j=0;j<paramTypes.length;j++)
            {
                if(j>0) System.out.print("'");
                System.out.print(paramTypes[j].getName());
            }
            System.out.println(");");
    }
    }
     
    public static void printMethods(Class c1)
    {
        Method[] methods=c1.getDeclaredMethods();
   
            
            for(Method m:methods)
            {
                Class retType =m.getReturnType();
                String name=m.getName();
               
                System.out.print("  "+Modifier.toString(m.getModifiers()));
                System.out.print(" "+retType.getName()+"  "+name+"(");
            
            Class [] paramTypes=m.getExceptionTypes();
            for(int j=0;j<paramTypes.length;j++)
            {
                if(j>0) System.out.print(", ");
                System.out.print(paramTypes[j].getName());
            }
            System.out.println(")");
        }
    }
   
   
    public static void printFieldes(Class c1)
    {
        Field[] fields=c1.getDeclaredFields();
        
        for(Field f:fields)
        {
            Class type =f.getType();
            String name=f.getName();
            System.out.print("  "+Modifier.toString(f.getModifiers()));
            System.out.println(" "+type.getName()+" "+name+";");
        }
    }
    public static void main(String[] args){
    String name;
    if(args.length>0)
        name=args[0];
    else
    {
        Scanner in =new Scanner(System.in);
        System.out.println("Enter class name (E.g. java.util.Date):");
        name=in.next();
    }
    try
    {
        Class  c1=Class.forName(name);
        Class  superc1=c1.getSuperclass();
        System.out.print("class"+ name);
        if(superc1 != null && superc1 != Object.class)
           System.out.print("extends"+superc1.getName());
         System.out.print("\n{\n");
         printConstructors(c1);
         System.out.println();
         printMethods(c1);
         System.out.println();
         printFieldes(c1);
         System.out.println("]");
    }catch(ClassNotFoundException e){e.printStackTrace();}
    System.exit(0);
   

    }
}




at ReflectionTest.main(ReflectionTest.java:73)说是 这个错误
搜索更多相关的解决方案: Class  name  forName  

----------------解决方案--------------------------------------------------------
错误信息是什么?
----------------解决方案--------------------------------------------------------
应该没有错的,要不就是arg[0]设置有误,else就是输入文件不存在!
----------------解决方案--------------------------------------------------------
程序正常, 没有错误
----------------解决方案--------------------------------------------------------
  相关解决方案