Java ResourceManager:找不到资源';index.vm';在任何资源加载器中
我正在尝试用Velocity模板创建一个简单的java项目,但我不断地遇到错误-Java ResourceManager:找不到资源';index.vm';在任何资源加载器中,java,velocity,Java,Velocity,我正在尝试用Velocity模板创建一个简单的java项目,但我不断地遇到错误- ResourceManager : unable to find resource 'index.vm' in any resource loader. index.vm与类文件一起存在。我尝试过其他几种选择,但都不起作用 我查阅了以下资源: 类文件: VelocityEngine velocityEngine = new VelocityEngine(); velocityE
ResourceManager : unable to find resource 'index.vm' in any resource loader.
index.vm与类文件一起存在。我尝试过其他几种选择,但都不起作用
我查阅了以下资源:
类文件:
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());
我尝试将以下属性添加到velocity上下文中,但它不起作用
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中成功运行它,但在将项目导出为runnable 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)
基本上,我认为你需要做的就是移动线路:
Template t = velocityEngine.getTemplate("index.vm");
在velocity引擎初始化之后,进一步向下
如果它仍然不起作用,您的index.vm
在哪里
如果它是一个文件,那么您将丢失以下内容:
velocityEngine.setProperty("file.resource.loader.path", "/some/path/");
如果它是类路径中的资源,则在index.vm
前面可能缺少/
:
Template t=velocityEngine.getTemplate('/index.vm')在更改路径并添加这4个属性后,我可以在eclipse中运行,但在执行jar时仍然会出现相同的错误。@NaiveCoder您没有回答我的问题:模板在哪里?在jar内部还是在文件系统中?@Claude Brisson,它位于maven项目的resources文件夹(resources/templates/index.vm)中。模板不在jar文件中。但是当将maven项目导出为jar并尝试执行jar时,会出现错误“Resoruce not found”@NaiveCoder您并没有真正回答我的问题。你打算做什么?你把模板放在罐子里了吗?如果是这样的话,您可能会在pom中缺少将包含在jar中的模板的适当的
部分。否则,即如果希望应用程序在文件系统上找到模板,则必须配置file.resource.loader.path
属性。这有意义吗?我在同一个maven项目的resources\templates文件夹中创建了一个简单的maven项目,其中包含一个类文件和一个模板。该项目在EclipseIDE中运行良好,它能够找到vm文件并对其进行处理。现在,将项目导出为包含所有依赖项的jar。jar包含类文件和模板的所有内容。使用java-jar在命令行上运行jar,我得到了ResourceNotFound错误。希望这有帮助。