【发布时间】:2023-03-13 05:04:01
【问题描述】:
我有一个程序使用的库。该库在资源文件夹中加载一个特殊目录。
在图书馆里我有方法
public class DataRegistry{
public static File getSpecialDirectory(){
String resourceName = Thread.currentThread().getContextClassLoader().getResource("data").getFile().replace("%20", " ");
File file = new File(resourceName);
return file;
}
}
在我的程序中,我有 main 方法
public static void main(String args[]){
System.out.println(Data.getSpecialDirectory());
}
当我在数据程序的 junit 测试中执行 getSpecialDirectory() 时,资源被获取并且一切正常。
当我在数据程序(导入的 jar)之外的 main 方法中执行 getSpecialDirectory() 时,我得到的是 jars 数据目录,而不是执行线程的程序所期望的目录。
我认为获取父类加载器会解决这个问题......我相信我在这里的理解可能有一个基本问题。
为了清楚起见:
(库) 该文件第 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
【问题讨论】:
-
你想达到什么目的?获取相对于您的 DataMojo 类的文件夹的路径?
标签: java jar classloader