Every now and then I need to use the Velocity templating. And the first thing I always experience is that nasty failure.

SEVERE: ResourceManager : unable to find resource 'template.vm'
 in any resource loader.
Exception in thread "main"
org.apache.velocity.exception.ResourceNotFoundException:
 Unable to find resource 'template.vm'
...

Velocity is so picky and always complains about the .VM template not being found. And I always start fiddling around, making sure file name is correct, making sure the VM file is right file location, class path location and yet same cruel error ResourceNotFoundException…

The answer for “properly” configuring Velocity and making it happy is bellow:

Properties p = new Properties();

// Uncomment if template.vm is being loaded from file system
//p.setProperty( "resource.loader", "file" );
// absolute or relative path
//p.setProperty( "file.resource.loader.path", "./src" );
//p.setProperty( "file.resource.loader.class",
   "org.apache.velocity.runtime.resource.loader.FileResourceLoader" );

// Uncomment if "template.vm" is being loaded from class path, jar, zip,..
//p.setProperty( "resource.loader", "class" );
//p.setProperty( "class.resource.loader.class",
  "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );

VelocityEngine ve = new VelocityEngine();
ve.init(p);
Template t = ve.getTemplate("template.vm");

All said, I am not sure why Velocity team designed it in such a strict way. Instead the engine should be smart enough to find the template directly from Class path location or File resource. Why so difficult?