0

我已下载该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. 有谁能够帮我?

4

1 回答 1

1

根据的javadoc URLClassLoader

[...] (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

于 2013-09-17T13:21:44.317 回答