I'm trying to find installed scripting engines with the Java Scripting API but something is going wrong.
In this demo program I used the ScriptEngineManager to retrieve factories and list all the engines I have. After that I try to get it by name, but the result is null.
public class App {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
List<ScriptEngineFactory> factories = manager.getEngineFactories();
for (ScriptEngineFactory factory : factories) {
System.out.println("\nName : " + factory.getEngineName());
System.out.println("Version : " + factory.getEngineVersion());
System.out.println("Language name : " + factory.getLanguageName());
System.out.println("Language version : " + factory.getLanguageVersion());
System.out.println("Extensions : " + factory.getExtensions());
System.out.println("Mime types : " + factory.getMimeTypes());
System.out.println("Names : " + factory.getNames());
ScriptEngine engine = manager.getEngineByName(factory.getEngineName());
if (engine == null) {
System.out.println("Impossible to find the engine with name " + factory.getEngineName()+"\n");
}
}
}
}
and the result is :
Version : 1.7 release 3 PRERELEASE
Language name : ECMAScript
Language version : 1.8
Extensions : [js]
Mime types : [application/javascript, application/ecmascript, text/javascript, text/ecmascript]
Names : [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]
Impossible to find the engine with name Mozilla Rhino
Name : jython
Version : 2.7.0
Language name : python
Language version : 2.7
Extensions : [py]
Mime types : [text/python, application/python, text/x-python, application/x-python]
Names : [python, jython]
Impossible to find the engine with name jython
Check the documentation of the getnames()
method of ScriptEngineFactory
:
Returns an immutable list of short names for the ScriptEngine, which may be used to identify the ScriptEngine by the ScriptEngineManager.
that is, one of these names must be used:
...
System.out.println("Names : " + factory.getNames());
ScriptEngine engine = manager.getEngineByName(factory.getNames().get(0));
...