简体   繁体   English

Java jar在Maven资源文件夹中找不到文件

[英]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.

相关问题 我在J/connector的文件夹里没有找到mysql-connector-java-version.jar文件 - I didn't find the mysql-connector-java-version.jar file in the folder of J/connector Java无法在资源文件夹中找到文本文件 - Java can't find text file in resource folder Java类加载器无法在jar文件中查找资源 - Java classloader not able to find resource in jar file 找不到资源文件夹中的文件 - java - Can not find file that is in resource folder - java 为什么Java在主源文件夹中找不到文件? - Why can't Java find a file inside of the main source folder? 文件写入jar中的资源文件夹 - File Write in resource folder in jar Heroku。 Java。 找不到资源。 我部署的应用程序在资源文件夹中找不到文件 - Heroku. Java. Unable to find resource. My deployed app can't find file in resource folder PMD + Maven + JAVA错误::无法找到资源rulesets / comments.xml。 确保资源是有效的文件或URL,或者位于CLASSPATH上 - PMD+Maven+JAVA Error:: Can't find resource rulesets/comments.xml. Make sure the resource is a valid file or URL or is on the CLASSPATH 如何使用 Maven 将所有必需的 JAR 文件放在最终 JAR 文件内的库文件夹中? - How do I put all required JAR files in a library folder inside the final JAR file with Maven? Java找不到杰克逊罐子文件 - Java can't find jackson jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM