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

不羁的心 提交于 2019-12-12 06:49:35

问题


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


回答1:


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())



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/44405897/org-apache-velocity-exception-resourcenotfoundexception-unable-to-find-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!