Jar类路径资源读取失败,这是从其他可执行jar触发的 [英] Jar classpath resources read failing which is triggerred from other executable jar
问题描述
参考链接:如何从Java jar文件读取资源文件?
我正在尝试使用您的代码库并尝试读取其中的内容 sample.csv
驻留在我的项目目录 src / main / resources
中。我无法读取内容,它说无法读取文件。输出:
I am trying using your code base and trying to read content of sample.csv
which is residing in my project directory src/main/resources
. I am unable to read the content, it says can not read file. Output:
[无法读取文件:
sample.csv
]
//此项在检查 / *后添加到了while循环中,如果它是目录,则跳过它。 * /
//This is added within your while loop after this check /* If it is a directory, then skip it. */
我的意思是,当检测到文件时,接下来是我下面的代码片段被添加来读取文件内容
I mean when file is detected then next is my below code snippet added to read the file content
if(entry.getName().contains("sample.csv")) {
File f1 = new File("sample.csv");
if(f1.canRead()) {
List<String> lines = Files.readAllLines(f1.toPath());
System.out.println("Lines in file: "+lines.size());
} else {
System.out.println("Can not read file: "+entry.getName());
}
}
有人可以教育我在这里做错什么吗,我如何使其工作?
Can anyone educate me what I am doing wrong here, how can I make it working?
我的要求是:
- (我的微服务)
Service.jar
在其pom中导入Parser.jar
库。 xml - (我的库)-
Parser.jar
具有FnmaUtils-3.2-fieldMapping.csv $
src / main / resources
目录中的c $ c>文件 -
有一个
FnmaUtils
类在其构造函数中加载FnmaUtils-3.2-fieldMapping.csv
的类,该类属于Parser.jar
-在这里我试图读取内容FnmaUtils-3.2-fieldMapping.csv
,此步骤一直失败,并出现以下错误,尝试了显示的所有可能选项[如何执行我从Java jar文件中读取了资源文件吗?
- (My micro-service)
Service.jar
importsParser.jar
library in its pom.xml - (My library) -
Parser.jar
hasFnmaUtils-3.2-fieldMapping.csv
file insrc/main/resources
directory There is a
FnmaUtils
class that loads theFnmaUtils-3.2-fieldMapping.csv
within its constructor, this class is part ofParser.jar
- Here I am trying to read the contentFnmaUtils-3.2-fieldMapping.csv
, this step is keep failing with below error, tried all possible options shown in [How do I read a resource file from a Java jar file?
public FnmaUtils() {
String mappingFileUrl = null;
try {
Resource resource = new ClassPathResource("FnmaUtils-3.2-fieldMapping.csv");
mappingFileUrl = resource.getFile().getPath();
loadFnmaTemplate(mappingFileUrl);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error loading fnma template file ", e);
}
}
获取错误:
java.io.FileNotFoundException: class path resource [`FnmaUtils-3.2-fieldMapping.csv`] cannot be resolved to absolute file path because it does not reside in the file system: `jar:file:/home/ravibeli/.m2/repository/com/xxx/mismo/util/fnma-parser32/2018.1.0.0-SNAPSHOT/fnma-parser32-2018.1.0.0-SNAPSHOT.jar!/FnmaUtils-3.2-fieldMapping.csv`
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:218)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at com.xxx.fnma.util.FannieMaeUtils.<init>(FannieMaeUtils.java:41)
at com.xxx.fnma.processor.FNMA32Processor.<init>(FNMA32Processor.java:54)
at com.xxx.fnma.processor.FNMA32Processor.<clinit>(FNMA32Processor.java:43)
这里出了什么问题?
推荐答案
尝试
InputStream in = this.getClass()。getClassLoader()
.getResourceAsStream( SomeTextFile.txt);
InputStream in = this.getClass().getClassLoader() .getResourceAsStream("SomeTextFile.txt");
请确保资源位于类路径中。
Be sure the resource is in your classpath.
这篇关于Jar类路径资源读取失败,这是从其他可执行jar触发的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!