我已下载aspectJ.jar
文件并将其解压缩到临时目录(此jar文件仅用于测试目的)。
我有以下代码:
public class TestUrlClassLoader {
public static void main(String [] args) throws MalformedURLException, ClassNotFoundException {
File file = new File("C:\\temp\\org\\aspectj\\lang\\JoinPoint.class");
URLClassLoader loader = URLClassLoader.newInstance(new URL[]{file.toURI().toURL()});
Class<?> aClass = loader.loadClass("org.aspectj.lang.JoinPoint");
System.out.println(aClass.toString());
}
}
存在JoinPoint
类的文件,但我得到ClassNotFoundException
。有人能帮助我吗?
答案 0 :(得分:1)
根据URLClassLoader
的javadoc,
[...] (URLClassLoader) is used to load classes and resources from a search path of URLs referring to both JAR files and directories. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed.
您应该删除JoinPoint.class
的完整路径并尝试使用要添加到类路径的目录,例如
File file = new File("C:\\temp");
URLClassLoader loader = URLClassLoader.newInstance(new URL[]{file.toURI().toURL()});
对javadoc的引用: http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html