首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

URLClassLoader 读取任意目录上的class

2012-11-04 
URLClassLoader 读取任意目录下的classpublic static void main(String[] args) throws IOException,Class

URLClassLoader 读取任意目录下的class

public static void main(String[] args) throws IOException,ClassNotFoundException {try {URL[] urls = new URL[] { new URL("file:/"+ System.getProperty("user.dir") + "/webroot/") };System.out.println(urls[0].getPath());URLClassLoader ucl = new URLClassLoader(urls);Class c = ucl.loadClass("PrimitiveServlet");System.out.println(c.getName());} catch (ClassNotFoundException e) {System.out.println(e.toString());}}


今天试验了半天,需要注意的是loadClass函数只能读取.class文件,如果是.java文件会报告ClassNotFound异常,需要先对其进行编译。

另外URLClassLoader 读取的任意目录也包括.jar

热点排行