nKognito
nKognito

Reputation: 6363

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??

Upvotes: 0

Views: 159

Answers (2)

Sainik Kumar Singhal
Sainik Kumar Singhal

Reputation: 801

It all depends on your class loader.If your class loader is not able to locate the resource it will return null.Try to load some resource which exist on your application's classpath. eg "myfolder/myfile.txt" where myfolder exist in root of your classpath.

See http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298888

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.

Upvotes: 1

Related Questions