【问题标题】:ResourceManager : unable to find resource 'index.vm' in any resource loaderResourceManager:无法在任何资源加载器中找到资源“index.vm”
【发布时间】: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)

【问题讨论】:

    标签: java velocity


    【解决方案1】:

    基本上,我认为你需要做的就是移动线路:

    Template t = velocityEngine.getTemplate("index.vm");    
    

    进一步向下,在速度引擎初始化之后。

    如果还是不行,你的index.vm在哪里?

    如果它是一个文件,那么你缺少类似的东西:

    velocityEngine.setProperty("file.resource.loader.path", "/some/path/");
    

    如果它是类路径中的资源,您可能在index.vm 前面缺少/

    模板 t = velocityEngine.getTemplate('/index.vm')

    【讨论】:

    • 更改路径并添加这 4 个属性后,我可以在 eclipse 中运行,但是在执行 jar 时仍然会出现相同的错误。
    • @NaiveCoder 你没有回答我的问题:模板在哪里?在 jar 中还是在文件系统中?
    • @Claude Brisson,它位于 maven 项目的资源文件夹(resouces/templates/index.vm)中。模板不在 jar 文件中。但是当将 maven 项目导出为 jar 并尝试执行 jar 时,会出现错误“找不到资源”
    • @NaiveCoder 你并没有真正回答我的问题。你打算做什么?罐子里有模板吗?如果是这样,您可能在 pom 中缺少正确的 <resources> 部分,以便将模板包含在您的 jar 中。否则,如果您希望您的应用程序在文件系统上找到模板,那么您必须配置 file.resource.loader.path 属性。这有意义吗?
    • 我在同一个 maven 项目的 resources\templates 文件夹中创建了一个简单的 maven 项目,其中一个类文件和一个模板。该项目在 Eclipse IDE 中运行良好,它能够找到 vm 文件并对其进行处理。现在,将项目导出为包含所有依赖项的 jar。 jar 包含所有类文件和模板。使用 java -jar 在命令行上运行 jar,我得到资源未找到错误。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2022-10-30
    相关资源
    最近更新 更多