gpt4 book ai didi

java - 为什么 Velocity "Velocity is not initialized correctly."会出现此错误?

转载 作者:行者123 更新时间:2023-12-02 11:01:01 27 4
gpt4 key购买 nike

我在扩展 AbstractProcessor 的注释处理器内初始化速度引擎,如下所示:

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{

String fqClassName = null;
String className = null;
String packageName = null;
Map<String, VariableElement> fields = new HashMap<String, VariableElement>();
Map<String, ExecutableElement> methods = new HashMap<String, ExecutableElement>();

...

if (fqClassName != null)
{

Properties props = new Properties();
URL url = this.getClass().getClassLoader().getResource("velocity.properties");

VelocityContext velocityContext = null;
Template template = null;
JavaFileObject javaFileObject;
Writer writer;

try
{
processingEnv.getMessager().printMessage(Kind.NOTE, "Before");
props.load(url.openStream());

processingEnv.getMessager().printMessage(Kind.NOTE, "Props: " + props);

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "classpath");
velocityEngine.init();
...
} catch (Exception e1)
{
processingEnv.getMessager().printMessage(Kind.ERROR,
"Exception: " + e1.getMessage() + " : " + e1.getStackTrace());
}

}

return false;

}

调用init()方法时发生异常。

我将带有 Velocity jar 文件的处理器导出为 jar 文件,并将 eclipse 配置为将其用作注释处理器。

最佳答案

您正在 props 变量中加载自定义属性,但从未将它们传递给 Velocity。

所以你应该简单地做类似的事情:

velocityEngine.init(props)

关于java - 为什么 Velocity "Velocity is not initialized correctly."会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51321260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com