Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jar没有';在maven资源文件夹中找不到文件_Java_Eclipse_Maven_Nullpointerexception_Invocationtargetexception - Fatal编程技术网

Java jar没有';在maven资源文件夹中找不到文件

Java jar没有';在maven资源文件夹中找不到文件,java,eclipse,maven,nullpointerexception,invocationtargetexception,Java,Eclipse,Maven,Nullpointerexception,Invocationtargetexception,我使用maven,在名为src/main/resources的sourcefolder中有一个文本文件,当我尝试从本地系统的文件夹中加载文件时,它工作正常 BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/stopwords_german.txt"))); 但是,当我在Eclipse中将项目导出为Runnable Jar并在我的服务器上启动

我使用maven,在名为
src/main/resources
的sourcefolder中有一个文本文件,当我尝试从本地系统的文件夹中加载文件时,它工作正常

BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/stopwords_german.txt")));
但是,当我在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)
线程“main”java.lang.reflect.InvocationTargetException中的异常 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:622) 位于org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58) 原因:java.lang.NullPointerException 在java.io.Reader上。(Reader.java:78) 位于java.io.InputStreamReader。(InputStreamReader.java:72) 在de.test.dataminer.helper.FilterText.initStopwords(FilterText.java:43)处 这是与BufferedReader的连线

我在这里看到了非常类似的问题,但没有解决任何问题,因此我测试了如何解决它(喜欢尝试使用classloader)。有人拿指针吗

    <resources>
      <resource>
        <directory>/home/johnny/workspace/dataminer/src/main/resources</directory>
      </resource>
    </resources>

/home/johnny/workspace/dataminer/src/main/resources

检查:只需用7zip左右打开jar(ZIP格式)。文件名应区分大小写(而Windows不区分大小写)。这也可以通过在IDE外部运行jar来测试

备注:将编码添加到InputStreamReader,否则将采用当前操作系统编码-不可移植

BufferedReader reader = new BufferedReader(
        new InputStreamReader(
            this.getClass().getResourceAsStream("/stopwords_german.txt"),
            "Cp1252"));

如果一切都合适,仍然有可能出错:
getClass()
可能是(通过继承)jar之外的某个类。然后,您可以执行类似于
SomeClassInJar.class.getResource
,或者使用
ClassLoader.getResourceAsStrean(“stopwords\u german.txt”)
,前面没有斜杠。

您是否考虑过文件在类路径中的位置,尝试访问该文件?也就是说,如果它在
资源中,而不是在子文件夹中,比如
新文件(“filename.txt”)
,Eclipse可能不会像maven那样打包jar。由于您使用的是maven,请尝试使用maven shade插件:能否添加与打包资源相关的pom.xml的一部分?我的意思是,插件本身和标签。问题也可能存在。添加了resources标记我查看了.jar,dir是/resources,因此我将其添加到文件路径前面,出于某种原因,结果是……正如我前面提到的,我已经尝试使用Classloader