【发布时间】:2020-09-21 03:23:49
【问题描述】:
我正在尝试使用 Velocity 模板创建一个简单的 java 项目,但我不断收到错误 -
ResourceManager : unable to find resource 'index.vm' in any resource loader.
index.vm 与 Class 文件一起存在。我尝试了其他几个选项,但没有任何效果。
我查看了以下资源:
http://velocity.apache.org/engine/1.7/user-guide.html
Velocity Unable To find Resources
类文件:
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class, file");
velocityEngine.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Template t = velocityEngine.getTemplate("\\templates\\index.vm");
VelocityContext vContext = new VelocityContext();
velocityEngine.init();
vContext.put("name", "World");
StringWriter writer = new StringWriter();
t.merge(vContext, writer);
System.out.println(writer.toString());
我尝试将以下属性添加到速度上下文,但它不起作用。
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
更新 1: 更改路径后,我能够在 Ec Lipse 中成功运行它,但在将项目导出为可运行 jar 或 maven shaded jar 后,我仍然遇到相同的错误。 这是堆栈跟踪:
SEVERE: ResourceManager : unable to find resource '\templates\index.vm' in any resource loader.
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
【问题讨论】: