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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 杜光 于 2013-8-4 10:08 编辑

public static void main(String[] args) throws Exception {
  InputStream inpt=new FileInputStream("config.properties");
  Properties properties = new Properties();
  properties.load(inpt);
  inpt.close();
  String className=properties.getProperty("className");
  
  Collection collection = (Collection) Class.forName(className).newInstance();
// Collection<Object> collection=new HashSet<Object>();
  ReflectPoint ref1 = new ReflectPoint(3, 3);
  ReflectPoint ref2 = new ReflectPoint(5, 5);
  ReflectPoint ref3 = new ReflectPoint(3, 3);
  collection.add(ref1);
  collection.add(ref2);
  collection.add(ref3);
  collection.add(ref1);
  System.out.println(collection.size());
}

config.properties 配置文件在项目下
是这么写的:
className=java.util.arrayList

运行main函数报错:

Exception in thread "main" java.lang.ClassNotFoundException: java.util.arrayList
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.itcase.day1.ReflectTest2.main(ReflectTest2.java:22)

是什么原因出的错呢?

评分

参与人数 1技术分 +1 收起 理由
杜光 + 1

查看全部评分

3 个回复

倒序浏览
那个ReflectPoint类暂时还没学到。
但是从报错来看,弱弱地问句。你导包了吗?
回复 使用道具 举报
本帖最后由 Jiewin 于 2013-8-3 23:51 编辑

如果已经导包了,就考虑你的配置文件存放的位置了
试一试把流那一句改为
InputStream inpt= 你的类名.class.getResourceAsStream("config.properties");
以后在关联配置文件时这样写比较安全

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马