1

I have a third party library that works with jUnit, but does not once deployed to tomcat. Digging into the code, I can see it fails here:

final String path = "/api-version.dat";
final InputStream stream = ClassLoader.class.getResourceAsStream(path);

The api-version.dat is located on jUnit test, but doing the same on Tomcat results in NullPointerException due to Tomcat unable to get the api-version file. This file exist and it is located inside the third party jar. What I have tried so far:

  • Check jar is imported into Tomcat: It is inside WEB-INF/lib, so I would say yes
  • Create an api-version.dat file and place it on my resources folder (I use maven). Does not work.

Any thoughts?

Eugenio Cuevas
  • 10,858
  • 3
  • 29
  • 51

1 Answers1

1

In a Java EE enviroments use:

final InputStream stream = Thread.currentThread()
                                 .getContextClassLoader()
                                 .getResourceAsStream(path);

See more in Difference between thread's context class loader and normal classloader.

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Thanks for this one, however, it is a third party library and I'm not able to modify it. Does it mean I cannot deploy it to a JEE environment? – Eugenio Cuevas Oct 16 '13 at 15:46
  • Right. Is it possible to inherit the class and override the method? Another way would be to inject a new method in the class, but it's complicated. – Paul Vargas Oct 16 '13 at 15:58
  • I have overridden the class, and that does not work either. It works if I return a String as the version, however I would like a cleaner way. – Eugenio Cuevas Oct 17 '13 at 08:27