package org.nhooo.example.script; import javax.script.ScriptEngineManager; import javax.script.ScriptEngineFactory; import java.util.List; public class GetSupportedScriptingEngine { public static void main(String[] args) { // 创建ScriptEngineManager的实例并获取列表 // 可用的ScriptEngineFactory。 ScriptEngineManager manager = new ScriptEngineManager(); List<ScriptEngineFactory> factories = manager.getEngineFactories(); for (ScriptEngineFactory factory : factories) { System.out.println( "EngineName = " + factory.getEngineName()); System.out.println( "EngineVersion = " + factory.getEngineVersion()); System.out.println( "LanguageName = " + factory.getLanguageName()); System.out.println( "LanguageVersion = " + factory.getLanguageVersion()); System.out.println( "Extensions = " + factory.getExtensions()); List<String> names = factory.getNames(); for (String name : names) { System.out.println("Engine Alias = " + name); } } } }
上面的代码产生以下信息。
EngineName = Mozilla Rhino EngineVersion = 1.7 release 3 PRERELEASE LanguageName = ECMAScript LanguageVersion = 1.8 Extensions = [js] Engine Alias = js Engine Alias = rhino Engine Alias = JavaScript Engine Alias = javascript Engine Alias = ECMAScript Engine Alias = ecmascript
当前,如您在上述程序的结果中所见,Mozilla Rhino是JDK 6库中提供的唯一脚本引擎。您还可以看到工厂提供了许多引擎名称别名,这些别名可以帮助您获取JavaScript编程语言的引擎。