Details
Description
I just spent about two hours ferreting out this obnoxious bug, but it should be
trivial to fix.
When configuring my velocity.properties file, I had the lines (taken verbatim
from the documentation):
- specify the resource loaders to use
resource.loader = class
class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class =
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.cache = false
I was consistently able to load a resource file from the classpath with
getClass().getClassLoader().getResourceAsStream("/my/package/foo.vm") but
Velocity consistently failed with the following stack trace:
Caused by: org.apache.velocity.exception.ResourceNotFoundException: Unable to
find resource '/gov/noaa/nndc/idb/render/vm/field/textField.vm'
at
org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:458)
at
org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:341)
at
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:831)
at
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:813)
at
org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java:285)
at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:469)
at gov.noaa.nndc.idb.IdbInitServlet.initVelocity(IdbInitServlet.java:241)
... 47 more
After much ado and hacking around, here is some of my debugging code and the output:
System.out.println("Resource Loaders: " +
Velocity.getProperty(RuntimeConstants.RESOURCE_LOADER));
System.out.println("Loader class: " +
Velocity.getProperty("class.resource.loader.class"));
Resource Loaders: [class ]
Loader class: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
It turns out that that there was an errant space in the properties file after
the word "class" and this was causing the classpath resource loader not to be
found. Velocity should trim this whitespace when reading the properties file!