A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王红霞 中级黑马   /  2012-7-24 19:52  /  1496 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王红霞 于 2012-7-25 18:53 编辑

class Test{

           public void run(){
                  
                   System.out.println("welcome to heima!");
                  
           }

}
public class PropertiesDemo {
        
         
          public static void main(String[] args)throws Exception{

                     InputStream  in= PropertiesDemo.class.getClassLoader()
                                     .getResourceAsStream("prop.properties");

                                Properties pro =  new  Properties();
                                pro.load(in);//文件正常配置,可是这个出错?
                                in.close();
                           
                                 String className = pro.getProperty("prop");
                                 Class c=Class.forName(className);

  
                                 Object ob=c.newInstance();
                                 Method m=c.getMethod("run");
                                 m.invoke(ob);
               
                        
                                 
                                 /*  Enumeration en = pro.propertyNames();
                                   while (en.hasMoreElements()) {
                                          
                                    String key = (String) en.nextElement();
                                    String Property = pro.getProperty(key);
                                    System.out.println(key + Property);
*/

                                 //  }
                        }
        }

1 个回复

倒序浏览
本帖最后由 Mrng8888 于 2012-7-24 20:10 编辑

我估计很有可能是你的properties文件里面的prop的value写错了,一定要写完整类名
  1. package test1;

  2. import java.io.InputStream;
  3. import java.lang.reflect.Method;
  4. import java.util.Properties;

  5. public class PropertiesDemo {

  6.         //如果是独立的一个类就是 包名.类名
  7.         //如果是静态内部类,就是 包名.外层类名$类名,比如下面这个Test类
  8.         //如果是一个普通的内部类,不好意思,不能直接对他实例化,内部类实例要依赖外部类实例
  9.         public static class Test {
  10.                 public void run() {
  11.                         System.out.println("welcome to heima!");
  12.                 }
  13.         }

  14.         public static void main(String[] args) throws Exception {
  15.                 InputStream in = PropertiesDemo.class.getClassLoader()
  16.                                 .getResourceAsStream("prop.properties");

  17.                 Properties pro = new Properties();
  18.                 pro.load(in);
  19.                 in.close();

  20.                 String className = pro.getProperty("prop");
  21.                 Class c = Class.forName(className);

  22.                 Object ob = c.newInstance();
  23.                 Method m = c.getMethod("run");
  24.                 m.invoke(ob);
  25.         }
  26. }

  27. properties文件
  28. prop=test1.PropertiesDemo$Test
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马