开发者

What's wrong with URLClassLoader when working with directories?

This is my code:

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()));
开发者_如何学C

Doesn't work. getResource() returns NULL. Why?


Because the resource names are always relative to the classpath you constructed the class loader with. You can't use an absolute name (that is, one that starts with /).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜