import java.util.*;
 
public class TestClassForName {
    
    public static void main(String[] args) {
    
     Class c1 = Class.forName("java.util.Date");
    }
}
以上就是全部的代码,我很郁闷的是不能运行,找了很多很多的资料,都没有说为什么不能运行。
如果改成 java.util.Date d = new java.util.Date();就可以运行。唉...,自学的新人伤不起啊

解决方案 »

  1.   

    Class.forName外面要加try catch
    import java.util.*;
     
    public class TestClassForName {
        
        public static void main(String[] args) {
            
            try {
    Class c1 = Class.forName("java.util.Date");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
        }
    }
      

  2.   

    非常非常感谢,但不明白的是为什么会这样一旦我不加 try catch, 就会报错, 我估计是我的基础太差了。再次感谢您的帮助
    import java.util.*;
     
     
     
    public class TestClassForName {
        
        public static void main(String[] args) {
        
            try {
    Class<?> test = Class.forName("CF.TestCF");
    System.out.println("it means ok if gose there");
            } catch (ClassNotFoundException e) {
                System.out.println("error in TestCF");
            }
        }
    }
      

  3.   

    楼主 这个是因为执行你的语句要报异常 你必须处理异常或者抛出  目测你不是用IDE大型开发工具写的代码 你用myeclipse会省很多事