Velocity can’t find resource

Something is wrong and it is very frustrating. I read on velocity’s homepage that when I run a webapp then some properties should be set. And I’ve done that but no matter what I do I keep getting the same error.
This is where I set the props and use velocity

public class ConfirmationMailGenerator implements MailGenerator {

    private BasicUser user;
    private String htmlTemplate = "HTMLConfirmationMailTemplate.vsl";
    private String plainTemplate = "PlainConfirmationMailTemplate.vsl";

    public ConfirmationMailGenerator(BasicUser user) {
        this.user = user;
    }

    public StringWriter generateHTML() throws Exception {
        Properties props = new Properties();
        props.setProperty("resource.loader", "wepapp");
        props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
        props.setProperty("webapp.resource.loader.path", "/WEB-INF/mailtemplates/");
        VelocityEngine engine = new VelocityEngine(props);
        VelocityContext context = new VelocityContext();

        engine.init();

        Map map = createDataModel();
        context.put("user", map);

        Template template = engine.getTemplate(htmlTemplate);
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        return writer;
    }
...
}

The files is of course saved in /WEB-INF/mailtemplates/.
If I use this I get this error:

SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader.
SEVERE: The log message is null.

Thank you for your time:)

Velocity can’t find resource with webApp

I’m trying to create a webApp using GlashFish server and Velocity. But i get the following error: SEVERE: ResourceManager : unable to find resource ‘temp.vm’ in any resource loader. SEVERE: org.apach

Apache velocity unable to find resource

I am trying to load a template with velocity. The only thing I have is the path of a file. but I can’t seem to load the File with this absolute path. How do you need to do this? The documentation is v

Cant find resource bundle messages

This question is very similar (if not the same as): Can’t find bundle for base name messages error ResourceBundle [messages] not found for MessageSource: Can’t find bundle for base name messages Bas

velocity template resource not found exception

Exception occurred: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource ‘ResourceLoader1.vm’ I am having ResourceLoader1.vm in /WEB-INF/templates, I stuck up with this pl

Velocity Template resource images

I have a spring configured web application and am using velocity engine and ITextRenderer to generate a PDF file. In my src/main/resources I have both my .vm file as well as a image file that I would

Cant find resource for bundle java.util.PropertyResourceBundle

I am getting an error and dont know whats the cause of the problem? java.util.PropertyResourceBundle, key connect_not_allowed Nested exception: Can’t find resource for bundle java.util.PropertyResour

Why this error comes in struts2 org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource ‘template.vm’

I am using Apache Velocity template engine for my web application html email utility. But the major problem i am facing is the resource path. I am using struts2 for my web application(Struts2 maven ar

using velocity to allocate resource time

Regards velocity: how can a team with a mix of skills be allocated time in the next Sprint based on velocity? Teams have different levels of developer as well as people more skilled in front end and

Velocity framework resource not found exception

I’m trying to run velocity email sender program in Java 1.6 but its not finding recourse… VelocityContext context = new VelocityContext(); context.put(name, mike); // Initialize the engine try

Using Velocity’s WebappResourceLoader with Spring

I’m trying to use Velocity to create an email template that is mailed by Spring’s JavaMailSender class. The resource loader that I decided to use to find the Velocity template in my web app is WebappR

Answers

Velocity is probably using the class loader to find those files. I’d recommend putting them in WEB-INF/classes, which is in the CLASSPATH by default.

You are using the Webapp resourceloader, which is intended for pages served by the Velocity Tools servlet. (It requires some special initialization to find the root of the servlet context).

I recommend you use the ClasspathResourceLoader, then put the files into WEB-INF/classes, or elsewhere in your classpath. This is really the most straight forward approach.

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

More info is here:

http://velocity.apache.org/engine/releases/velocity-1.6.4/developer-guide.html#Configuring_Resource_Loaders

and here:

http://velocity.apache.org/engine/releases/velocity-1.6.4/apidocs/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html

Will Glass answer is correct, but the configuration should be:

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

Note the class at the beginning of the second line. See the links provided by him for more details!.

Note: Making an answer instead of a comment due to privileges.

I am fine it as follow,

In velocity.properties file

resource.loader=class, file
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path=vm_template
runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
runtime.log.logsystem.log4j.category=velocity
input.encoding=UTF-8
output.encoding=UTF-8

And at my java class

import java.io.StringWriter;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.EscapeTool;
import org.apache.velocity.tools.generic.LoopTool;
import org.apache.velocity.tools.generic.MathTool;
import org.apache.velocity.tools.generic.NumberTool;
import org.apache.velocity.tools.generic.SortTool;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class VelocitySupport implements InitializingBean {
private static Logger log = Logger.getLogger(VelocitySupport.class);

@Autowired private Properties properties;

public final void afterPropertiesSet() throws Exception {
    location = location.replace("classpath:", "");
    Resource res = new ClassPathResource(location);

    Properties prop = new Properties();
    prop.load(res.getInputStream());

    String staticDir = System.getProperty("staticDir");
    String tempPath = prop.getProperty("file.resource.loader.path");
    tempPath = staticDir + "/" + tempPath;

    prop.setProperty("file.resource.loader.path", tempPath);
    Velocity.init(prop);
}

public static String merge(final String template, final VelocityContext vc) throws Exception {
    try {
        vc.put("date", new DateTool());
        vc.put("escape", new EscapeTool());
        vc.put("math", new MathTool());
        vc.put("number", new NumberTool());
        vc.put("iterate", new LoopTool());
        vc.put("sort", new SortTool());

        Template temp = Velocity.getTemplate(template);

        StringWriter sw = new StringWriter();
        temp.merge(vc, sw);
        sw.flush();

        return sw.toString();
    }
    catch (ResourceNotFoundException e) {
        log.error("", e);
        throw e;
    }
    catch (ParseErrorException e) {
        log.error("", e);
        throw e;
    }
}

private String location;

public final void setLocation(final String location) {
    this.location = location;
}
}

And insert VM arguments of project as follow..

-DstaticDir= "your directory for template path"

That may be helpful for you

For resolving this error –WEB-INF/classes and all the JARs in WEB-INF/lib are in the CLASSPATH. Try moving your folder with the .vm files under WEB-INF/classes –dont put the abolute path eg. if abc.vm file is in /public_html/WEB-INF folder then put path = “/public_html/WEB-INF/abc.vm” for velocity template path.