Search code examples
javaslf4jvelocityjsr233apache-velocity

Apache Velocity - Failed to eval script due to slf4j dependencies


Continue previous question I'm failing to execute scripting in velocity 2.0,

I use jars: velocity-engine-scripting-2.0.jar, velocity-engine-scripting-2.0.jar, commons-collections-3.2.2.jar

I'm trying to follow developer guide example:

ScriptEngineManager manager = new ScriptEngineManager();
manager.registerEngineName("velocity", new VelocityScriptEngineFactory());
ScriptEngine engine = manager.getEngineByName("velocity");


System.setProperty(VelocityScriptEngine.VELOCITY_PROPERTIES, "path/to/velocity.properties");
String script = "Hello $world";
Writer writer = new StringWriter();
engine.getContext().setWriter(writer);
Object result = engine.eval(script);
System.out.println(writer);

I'm getting slf4j error when init, I'm using slf4j-jdk14.jar. I didn't find solution fir this specific error even after adding slf4j-api-1.8.0-alpha2.jar

class org.apache.velocity.script.VelocityScriptEngine
java.lang.NoSuchMethodError: org.slf4j.Logger.trace(Ljava/lang/String;)V
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:233)
    at org.apache.velocity.script.VelocityScriptEngine.initVelocityEngine(VelocityScriptEngine.java:212)
    at org.apache.velocity.script.VelocityScriptEngine.compile(VelocityScriptEngine.java:299)
    at org.apache.velocity.script.VelocityScriptEngine.compile(VelocityScriptEngine.java:288)
  • Notice I don't use any logging in my class so I don't need any migration to slf4j.

EDIT 1:

I found interesting comment in slf4j FAQ about Velocity:

The logging strategy adopted by the Velocity project is a good example of the "custom logging abstraction" anti-pattern. By adopting an independent logging abstraction strategy, Velocity developers have made life harder for themselves, but more importantly, they made life harder for their users.

EDIT 2:

Velocity dependencies in Runtime are: slf4j-api 1.7.25 and commons-lang 3.5 which I added to classpath but still same error


Solution

  • The documentation excerpt you found in the FAQ is clearly outdated.

    The dependencies page clearly states that while slf4j-api is needed at compile and runtime, you also need to choose one of the slf4j bindings, for instance slf4j-simple which will log to stderr by default.