0

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

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • This error clearly saying dependency mismatch . IMO if you are using maven project you can exclude the `slf4` version from velocity. – soorapadman Aug 21 '17 at 07:13
  • I'm not in maven project and velocity is calling slf4j specifically so it can't be removed `org.slf4j.Logger.trace` – Ori Marko Aug 21 '17 at 07:23
  • I came across this issue specifically i can manage to fix by exclude . I really don't have idea . if you are not using maven. But definitely you must remove that dependency somehow otherwise will not work IMO – soorapadman Aug 21 '17 at 07:26

1 Answers1

1

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.

Claude Brisson
  • 4,085
  • 1
  • 22
  • 30
  • I added `slf4j-simple-1.7.25.jar` but still same issue – Ori Marko Aug 22 '17 at 05:09
  • Did you compile Velocity yourself from the sources? I ran your code without any problem (except it's `VELOCITY_PROPERTIES_KEY`). Maybe you forgot to recompile after changing the libs. – Claude Brisson Aug 22 '17 at 08:36