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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© nestor 中级黑马   /  2014-4-15 21:44  /  836 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 nestor 于 2014-4-16 09:14 编辑

代码:

  1. package fanShe;

  2. import java.io.*;
  3. import java.util.*;

  4. public class KuangJia {

  5.         public static void main(String[] args) throws Exception {
  6.                 // TODO Auto-generated method stub
  7.                 InputStream ips = new FileInputStream("src\\fanShe\\config.properties");
  8.                 Properties props = new Properties();
  9.                 props.load(ips);
  10.                 ips.close();
  11.                 String className = props.getProperty("className");
  12.                 // System.out.println(className);
  13.                 Collection collections = (Collection) Class.forName(className).newInstance();

  14.                 String pt1 = new String("a");
  15.                 String pt2 = new String("b");
  16.                 String pt3 = new String("a");

  17.                 collections.add(pt1);
  18.                 collections.add(pt2);
  19.                 collections.add(pt3);
  20.                 collections.add(pt1);

  21.                 System.out.println(collections.size());

  22.         }

  23. }

复制代码

报错:
  1. Exception in thread "main" java.lang.ClassNotFoundException: java.unil.ArrayList
  2.         at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  3.         at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  4.         at java.security.AccessController.doPrivileged(Native Method)
  5.         at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  6.         at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  7.         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  8.         at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  9.         at java.lang.Class.forName0(Native Method)
  10.         at java.lang.Class.forName(Class.java:190)
  11.         at fanShe.KuangJia.main(KuangJia.java:16)
复制代码

config.properties:
  1. className=java.unil.ArrayList
复制代码



2 个回复

倒序浏览
ArrayList完整类名弄错了是:java.util.ArrayList
回复 使用道具 举报
同楼上所说:java.util.ArrayList
顺便一提,一些自己出心打错的问题,先检查代码有没有拼写错误
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马