读书人

URLClassLoader 读取任意目录上的class

发布时间: 2012-11-04 10:42:41 作者: rapoo

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

读书人网 >编程

热点推荐