Search code examples
javaspringresourcesspring-bootclassloader

Loading resource fails at server-side


I have a library that load the resources in the following way:

BufferedReader r = new BufferedReader(
         new InputStreamReader(classLoader.getResourceAsStream(String.format("%s.txt", filename)), "UTF8")
);

Library's tests run fine.

After that another webapp (spring-boot) uses that library as a dependency (with Maven). But! If I run that webapp from Intellij - works fine. But when I upload it to server, or run it locally with java -jar it fails with:

Caused by: java.lang.NullPointerException: null
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:97)

Any ideas??


Solution

  • I guess this depends on which ClassLoader is used. I would suggest you define classLoader as

    ClassLoader classLoader = SomeClass.class.getClassLoader();
    

    where SomeClass is a class in the same jar that contains the '.txt' resources.