开发者

What's wrong with URLClassLoader when working with directories?

开发者 https://www.devze.com 2023-03-31 01:32 出处:网络
This is my code: File folder = /* the folder with a.txt file */ final URL[] urls = new URL[] { folder.toURI().toURL()

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

精彩评论

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