[英]Java jar didn't find file inside maven resource folder
I use maven and have a textfile in a sourcefolder called src/main/resources
when I try to load a file from within the folder on my local system it works well 当我尝试从本地系统上的文件夹内加载文件时,我使用maven并在名为
src/main/resources
文件夹中有一个文本文件
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/stopwords_german.txt")));
but as soon as I export the project as Runnable Jar in Eclipse and start the jar on my Server I get a Nullpointer exception 但是,一旦我在Eclipse中将项目导出为Runnable Jar并在我的服务器上启动jar,我就会收到Nullpointer异常
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:622)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at de.test.dataminer.helper.FilterText.initStopwords(FilterText.java:43)
It's the line with the BufferedReader. 这是BufferedReader所在的行。
I saw very similar questions here but nothing worked out so for that I tested to fix it ( like to try with classloader ). 我在这里看到了非常相似的问题,但没有解决,因此我进行了修复( 例如尝试使用classloader )。 Anyone a pointer?
有人指点吗?
<resources>
<resource>
<directory>/home/johnny/workspace/dataminer/src/main/resources</directory>
</resource>
</resources>
A Check: simply open the jar (ziip format) with 7zip or so. 检查:只需用7zip左右的高度打开罐子(Zip格式)。 The file name should be case-sensitive (whereas Windows is case-insensitive).
文件名应区分大小写(而Windows不区分大小写)。 This could also be tested by running the jar outside the IDE.
也可以通过在IDE外部运行jar来测试。
A remark: add the encoding to the InputStreamReader, otherwise the current operating system encoding is taken - non-portable. 备注:将编码添加到InputStreamReader,否则采用当前的操作系统编码-不可移植。
BufferedReader reader = new BufferedReader(
new InputStreamReader(
this.getClass().getResourceAsStream("/stopwords_german.txt"),
"Cp1252"));
If everything fits, still something might go wrong: getClass()
might be (maybe through inheritance) some class outside the jar. 如果一切合适,仍然可能会出问题:
getClass()
可能是(可能是通过继承)罐子外部的某个类。 Then you either do something like SomeClassInJar.class.getResource
, or use a ClassLoader.getResourceAsStrean("stopwords_german.txt")
without slash in front. 然后,您可以执行
SomeClassInJar.class.getResource
,或者使用ClassLoader.getResourceAsStrean("stopwords_german.txt")
而不在前面加斜杠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.