简体   繁体   中英

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates/email/test.vm'

My code as follow.

VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "class");
        ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.init();
        Template tempalte = ve.getTemplate("templates/email/test.vm");

I am getting following error.

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates/email/test.vm' at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:452) at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335) at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102).....

"template" folder is in my src folder.

If I directly put the velocity template file into src folder and update as "Template tempalte = ve.getTemplate("test.vm");" it works.

I am using Mac OS, Java 1.8, Eclipse Neon.

Please help me to solve this.

Thanks and Regards, IsuruJ

Try to set below variables and check it should work

ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
        ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
        ve.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
        ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");

And your velocity file should be under resources folder

The above way i have it my project if that not working try this

I believe your folder structure like /src/main/java/com/xyz/email/velocity/.

ve.velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");
  ve.velocityEngine.setProperty("classpath.resource.loader.class",ClasspathResourceLoader.class.getName())

Few Issues may cause it

  1. Templates folder is the root of velocity so removing it may work

    ve.getTemplate("/email/test.vm");

  2. File with uppercase which is ignored by windows as: folder Email or file Test.vm cannot be found

  3. Permissions of new folder/file not allowing read if user not owner

Thanks for your time. This is an ANT project. Mistakenly I couldn't write script to copy templates folder to WEB-INF/classes folder. That was the issue. Both @user7294900 and @soorapadman are correct. The code works now.

Really sorry for the inconvenience.

Best Regards, IsuruJ

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