Ant*_*ink 1 java jar classloader
我有一个程序使用的库.该库在resources文件夹中加载一个特殊目录.
在图书馆我有方法
public class DataRegistry{
public static File getSpecialDirectory(){
String resourceName = Thread.currentThread().getContextClassLoader().getResource("data").getFile().replace("%20", " ");
File file = new File(resourceName);
return file;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的程序中我有主要方法
public static void main(String args[]){
System.out.println(Data.getSpecialDirectory());
}
Run Code Online (Sandbox Code Playgroud)
当我在数据程序中的junit测试中执行getSpecialDirectory()时,将获取资源并且一切正常.
当我在数据程序(导入的jar)之外的main方法中执行getSpecialDirectory()时,我得到了jar数据目录,而不是执行该程序的程序所期望的目录.
我想让父类加载器能解决这个问题......我相信我的理解可能会有一个根本问题.
为清楚起见:
(图书馆)此文件的第15行:https://gist.github.com/AnthonyClink/11275442
(用法)此文件的第31行:https: //gist.github.com/AnthonyClink/11275661
我的poms可能与它有关,所以分享它们可能很重要:
(图书馆) https://github.com/Clinkworks/Neptical/blob/master/pom.xml
(用法) https://github.com/Clinkworks/Neptical-Maven-Plugin/blob/master/pom.xml
如果您只需要阅读资源的内容,则应使用java.lang.ClassLoader.getResourceAsStream(...)并从中读取内容.
请注意,无法更新类装入器资源的内容.