Java 脚本 API 无法按名称检索引擎
Java scripting API fails to retrieve engine by name
我正在尝试使用 Java 脚本 API 查找已安装的脚本引擎,但出了点问题。
在这个演示程序中,我使用 ScriptEngineManager 来检索工厂并列出我拥有的所有引擎。之后我尝试通过名称获取它,但结果为空。
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");
}
}
}
}
结果是:
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
查看ScriptEngineFactory
的getnames()
方法的文档:
Returns an immutable list of short names for the ScriptEngine, which may be used to identify the ScriptEngine by the ScriptEngineManager.
也就是说,必须使用以下名称之一:
...
System.out.println("Names : " + factory.getNames());
ScriptEngine engine = manager.getEngineByName(factory.getNames().get(0));
...
我正在尝试使用 Java 脚本 API 查找已安装的脚本引擎,但出了点问题。
在这个演示程序中,我使用 ScriptEngineManager 来检索工厂并列出我拥有的所有引擎。之后我尝试通过名称获取它,但结果为空。
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");
}
}
}
}
结果是:
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
查看ScriptEngineFactory
的getnames()
方法的文档:
Returns an immutable list of short names for the ScriptEngine, which may be used to identify the ScriptEngine by the ScriptEngineManager.
也就是说,必须使用以下名称之一:
...
System.out.println("Names : " + factory.getNames());
ScriptEngine engine = manager.getEngineByName(factory.getNames().get(0));
...