0

I am trying to get Apache Velocity up and running. I have a TestClass.class class in my my.test.package package.

public class TestClass {
    public static Template getTestTemplate() throws Exception {
        Velocity.init();
        return Velocity.getTemplate("MyTestTemplate.vm");
    } 
}

In the same location (my.test.package) I have the MyTestTemplate.vm file.

The above code causes an exception to be thrown, saying Unable to find resource 'MyTestTemplate.vm'. I am not sure what the issue is. Does Velocity not look for the file in the same package? (Note: I originally had the file in the resources folder, but put it under the same folder for testing purposes).

Snowy Coder Girl
  • 5,408
  • 10
  • 41
  • 72

1 Answers1

3

Okay, figured it out.

So I figured maybe Velocity was looking in my WEB-INF/classes folder. I looked in there only to discover that the MyTestTemplate.vm file wasn't there. Turned out I needed to update my Ant script that copied over the resources to include .vm files.

<include name="**/*.vm"/>

Then I needed to update my configuration so that Velocity knew to look in the classes folder.

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "class");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.init();

Then when you get the template, you just need to provide the path after the WEB-INF/classes part.

velocityEngine.getTemplate("path/to/resource/MyTestTemplate.vm");

I am sure there is some way to get the templates off of the file path, but I stopped caring ;)

Snowy Coder Girl
  • 5,408
  • 10
  • 41
  • 72
  • Thanks I was missing these two lines: velocityEngine.setProperty("resource.loader", "class"); velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); – chrisr Sep 06 '12 at 01:12