1

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

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

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.

I saw very similar questions here but nothing worked out so for that I tested to fix it (like to try with classloader). Anyone a pointer?

    <resources>
      <resource>
        <directory>/home/johnny/workspace/dataminer/src/main/resources</directory>
      </resource>
    </resources>
Johnny000
  • 2,058
  • 5
  • 30
  • 59
  • Have you tried to access a file considering its location in classpath? I.E., if it is in `resources`, not in subfolder, like `new File("filename.txt")`? – Alexey Malev May 11 '14 at 21:21
  • 1
    Eclipse is likely not packaging the jar as maven would. Since you are using maven, try using the maven-shade-plugin: http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html – Drew MacInnis May 11 '14 at 21:23
  • Can you add part of your pom.xml which relates to packaging resources? I mean, plugin itself and tag. The problem might be there also. – Alexey Malev May 11 '14 at 22:26
  • Added the resources tag – Johnny000 May 12 '14 at 19:22
  • I took a look into the .jar and the dir was /resources, so I added it in front of the filepath, for some reason it worked out.. – Johnny000 May 14 '14 at 14:21

1 Answers1

0

A Check: simply open the jar (ziip format) with 7zip or so. The file name should be case-sensitive (whereas Windows is case-insensitive). This could also be tested by running the jar outside the IDE.

A remark: add the encoding to the InputStreamReader, otherwise the current operating system encoding is taken - non-portable.

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. Then you either do something like SomeClassInJar.class.getResource, or use a ClassLoader.getResourceAsStrean("stopwords_german.txt") without slash in front.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138