I have to use a library that accepts path to a directory and loads it via File(path);. What should be the path in tomcat webapp context ?
I always work with classpaths, but this API is really not thought through…
I don’t have experience with java execution context in tomcat, but it seems to me a bad idea doing something like this
System.getProperty("catalina.base") + "webapps/app/WEB-INF/classes/profiles"
Does tomcat provides java execution (AKA $PWD) path for resources in application context ?
Use
ServletContext#getRealPath()to convert a webapp-relative path to an absolute disk file system path. Given your example, the following should do:Note that this requires that the WAR is expanded, otherwise it will return
null. Tomcat by default expands the WAR, but some other servletcontainers/configs don’t. Keep this in mind with regard to portability.