I need to load a file resource when starting my server. So I placed the file under webapp/WEB-INF.
The problem is that the unit tests fail.
I try to load the file using:
URL url = Utils.class.getClassLoader().getResource(fileName);
File file = new File(url.getFile());
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
But the class loader does not find the file. It seems like it is looking for it at
file:/C:/<local path>/<my project>/target/test-classes/
- There are different contexts for during normal mode and test mode
I tried also the solution suggested here but it didnt work
It is a relatively complex process how classpaths are overriden and resolved in tomcat, but the simple answer is that
WEB-INF
is not part of the classpath so you can't access it from your webapp like that.WEB-INF/classes
is where you can read files from.Probably you want to read through this q/a which should give you all the potential solutions