Search code examples
javajsr223mvel

MVEL script engine isn't discovered


I have an approprivate jar in my classpath,

    <dependency>
        <groupId>org.mvel</groupId>
        <artifactId>mvel2</artifactId>
        <version>2.4.4.Final</version>
        <type>jar</type>
    </dependency>

the jar does contain META-INF/services/javax.script.ScriptEngineFactory file which says org.mvel2.jsr223.MvelScriptEngineFactory, the latter file does look like a script engine factory.

Still

private final ScriptEngineManager _scripting = new ScriptEngineManager(null);
....
ScriptEngine engine = _scripting.getEngineByName("mvel");

returns null. Ive checked the code of the factory, the name mvel is valid:

https://github.com/mvel/mvel/blob/master/src/main/java/org/mvel2/jsr223/MvelScriptEngineFactory.java

My java is java-8-openjdk-amd64 on Ubuntu 18.04.2 LTS.

I can always add mvel manually to the script manager but should I? Currently only Nashorn is discovered


Solution

  • The script engine is registered through META-INF/services/javax.script.ScriptEngineFactory which contains the data:

    org.mvel2.jsr223.MvelScriptEngineFactory

    First, ensure that you are actually packaging a JAR that has this file and text. Beware packaging in a single large jar file may result in this file being overwritten or ignored -- if doing so, this file must contain all the scripting engines to be referenced by the application. Failure to find this file could also be the result of classloader mis-management.

    From Oracle: https://docs.oracle.com/javase/8/docs/api/javax/script/compact2-package-summary.html

    Script engine discovery and Metadata: Applications written to the Scripting API might have specific requirements on script engines. Some may require a specific scripting language and/or version while others may require a specific implementation engine and/or version. Script engines are packaged in a specified way so that engines can be discovered at runtime and queried for attributes. The Engine discovery mechanism is based on the Service discovery mechanism described in the Jar File Specification. Script engine implementing classes are packaged in jar files that include a text resource named META-INF/services/javax.script.ScriptEngineFactory. This resource must include a line for each ScriptEngineFactory that is packaged in the jar file. ScriptEngineManager includes getEngineFactories method to get all ScriptEngineFactory instances discovered using this mechanism. ScriptEngineFactory has methods to query attributes about script engine.