title: How to #include a file from a Velocity template using ClasspathResourceLoader
body: <p>I'm dealing with some Java code where Velocity 1.7 is set to retrieve templates through the ClasspathResourceLoader. Below is a stripped down sample of the code. It's from a Tapestry web application running on a Jetty server.</p>
  
  <p>The Java class, the templates and the file to be included are all are in the same folder "testpackage", so in the resulting JAR they are all in the same package "testpackage".</p>
  
  <p>The problem is that if the template contains an</p>
  
  <pre><code>#include("MyInclude.vm")
  </code></pre>
  
  <p>directive, Velocity cannot find "MyInclude.vm", and it throws a ResourceNotFoundException.</p>
  
  <p>Since in the argument of getTemplate I must prepend the package name to the template name, I also tried to do the same in the #include inside the template:</p>
  
  <pre><code>#include("testpackage/MyInclude.vm")
  </code></pre>
  
  <p>but the only difference is that the latter works if I run the web app from Eclipse, while the former doesn't work not even from Eclipse.
  If I build, deploy the JARs and run the web app from the deployment, both syntaxes fail with the same ResourceNotFoundException.</p>
  
  <p>The Velocity doc at <a href="http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html#Include" rel="noreferrer">http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html#Include</a> says: </p>
  
  <blockquote>
    <p>"For security reasons, the file to be included may only be under
    TEMPLATE_ROOT"</p>
  </blockquote>
  
  <p>This definitely could be the cause of my problem, but I haven't been able to find any further information about what TEMPLATE_ROOT actually is.</p>
  
  <p>It sounds a lot like an environment variable, but I don't know what I should set it to, since I'm using a ClasspathResourceLoader, and the file to be included is not an actual file located in a folder, it's inside the JAR containing the templates and the Java class (and all in the same package).</p>
  
  <p>I found TEMPLATE_ROOT mentioned in another question, <a href="https://stackoverflow.com/questions/435210/where-should-i-put-velocity-template-files-for-a-command-line-utility-built-with">Where should I put Velocity template files for a command line utility built with Maven?</a> , but it's related to using a FileResourceLoader. I need to keep using the ClasspathResourceLoader and I need all the files to be in the JAR, not outside it as normal files in some folder.</p>
  
  <h2>TestVelocity.java</h2>
  
  <pre><code>package testpackage;
  
  import java.io.StringWriter;
  import java.util.Properties;
  
  import org.apache.velocity.Template;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.app.VelocityEngine;
  
  public class TestVelocity
  {
    public static String getText()
    {
      String text = "";
  
      Properties properties = new Properties();
  
      properties.setProperty("resource.loader", "class");
  
      properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  
      VelocityEngine engine = new VelocityEngine();
  
      VelocityContext context = new VelocityContext();
  
      StringWriter writer = new StringWriter();
  
      try
      {
        engine.init(properties);
  
        // This works because the template doesn't contain any #include
        Template template = engine.getTemplate("testpackage/TemplateWithoutInclude.vm");
  
        // This causes ResourceNotFoundException: Unable to find resource 'TemplateWithInclude.vm'
        // Template template = engine.getTemplate("testpackage/TemplateWithInclude.vm");
  
        template.merge(context, writer);
  
        text = writer.toString();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      return text;
    }
  }
  </code></pre>
  
  <hr>
  
  <h2>TemplateWithoutInclude.vm</h2>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;p&gt;Hello&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <h2>TemplateWithInclude.vm</h2>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
          #include("MyInclude.vm")
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <h2>MyInclude.vm</h2>
  
  <pre><code>&lt;p&gt;
      Hello
  &lt;/p&gt;
  </code></pre>
  
  <hr>