这是我的代码:
File folder = /* the folder with a.txt file */
final URL[] urls = new URL[] {
folder.toURI().toURL()
};
ClassLoader loader = new URLClassLoader(urls);
assertThat(loader.getResource("/a.txt"), is(notNullValue()));
不起作用。 getResource()
返回NULL
。为什么?
最佳答案
因为资源名称始终与您构建类加载器所用的类路径相关。您不能使用绝对名称(即以 /
开头的名称)。
关于java - 使用目录时 URLClassLoader 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7219367/