java无法在可执行jar中加载资源
我的代码在Eclipse中按预期执行,但当我尝试将其导出到可执行jar时,它停止加载资源。我在这里看了很多例子,但似乎无法找出我做错了什么。我查看了jar内容,资源文件都在那里。jar中的层次结构是:
/src/code
/resources files
在Eclipse项目中,层次结构是
/src/classes
/resources/resource files
在程序启动时,我有一个ResourceLoader类加载我需要的所有资源。因为我有这么多的资源文件,所以我在代码中使用了以下方法:
private static Scanner loadScanner(String fileName, ClassLoader classLoader) throws IOException, NullPointerException {
File file;
Scanner sc;
try { //to load the resource file
file = new File(classLoader.getResource(fileName).getFile());
} catch (NullPointerException npe) {
throw new NullPointerException("File " + fileName + " not found in ResourceLoader.loadScanner()");
}
sc = new Scanner(file);
return sc;
}
我用如下方式调用该方法:
String fileName = "example.csv";
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
sc = loadScanner(fileName,classLoader);
我收到的错误消息是:
file:\C:\...\myjar.jar!\example.csv (The filename, directory name, or volume label syntax is incorrect)
Exception in thread "main" java.lang.NullPointerException
我的假设是jar代码正在寻找一个资源文件夹,但它不在那里。我需要如何修改我的代码或项目/jar规范以使其按我期望的方式工作?谢谢你的帮助
此外,出于教育目的,jar文件似乎抑制了异常。。。?如果找不到资源文件,为什么我的NullPointerException不能捕获触发器
共 (0) 个答案