简体   繁体   中英

Velocity Template - Exception in thread “main” org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource

I have tried running simple velocity template program in java eclipse and getting below error:

Sep 22, 2017 4:53:14 PM org.apache.velocity.runtime.log.CommonsLogLogChute log
SEVERE: ResourceManager : unable to find resource 'templates/HelloWorld.vm' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates/HelloWorld.vm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
    at com.sapient.velocity.HelloVelocity.main(HelloVelocity.java:18)

tried everything checking errors online stackoverflow:

my project structure:

在此处输入图片说明

HelloWorld.vm :

Velocity Template $helloWorld

HelloVelocity Class

public class HelloVelocity {

    public static void main(String[] args) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
        Template t = ve.getTemplate("templates/HelloWorld.vm");
        VelocityContext vc = new VelocityContext();
        vc.put("helloWorld", "Hello World!!!");
        StringWriter sw = new StringWriter();
        t.merge(vc, sw);
        System.out.println(sw);
    }
}

I am using Velocity 1.7 build.

Rename the file HelloWorld.vm in your resource folder instead of HelloWold.vm . There is a mismatch in the filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM