6.游戏特别离不开脚本(2)-获取脚本引擎
1 import java.util.List; 2 3 import javax.script.ScriptEngine; 4 import javax.script.ScriptEngineFactory; 5 import javax.script.ScriptEngineManager; 6 import javax.script.ScriptException; 7 8 public class GetSupportedScriptingEngine { 9 public static void main(String[] args) { 10 11 //创建一个ScriptEngineManager 12 ScriptEngineManager manager = new ScriptEngineManager(); 13 //取得所有ScriptEngineFactory 14 List<ScriptEngineFactory> factories = manager.getEngineFactories(); 15 //遍历输出 16 for (ScriptEngineFactory factory : factories) { 17 System.out.println("EngineName = " + factory.getEngineName()); 18 System.out.println("EngineVersion = " + factory.getEngineVersion()); 19 System.out.println("LanguageName = " + factory.getLanguageName()); 20 System.out.println("LanguageVersion = " + factory.getLanguageVersion()); 21 System.out.println("Extensions = " + factory.getExtensions()); 22 23 List<String> names = factory.getNames(); 24 for (String name : names) { 25 System.out.println("Engine Alias = " + name); 26 } 27 28 //通过factory获取ScriptEngine 29 30 // ScriptEngine scriptEngine = factory.getScriptEngine() ; 31 ScriptEngine scriptEngine = manager.getEngineByExtension("js") ;//其他方式获取 32 // ScriptEngine scriptEngine2 = manager.getEngineByName("JavaScript") ;//其他方式获取 33 try { 34 //脚本引擎执行JavaScript语句 35 scriptEngine.eval("var arr = new Array() ; arr.push(1); print(arr.pop())") ; 36 } catch (ScriptException e) { 37 e.printStackTrace(); 38 } 39 } 40 } 41 }
结果:
EngineName = Mozilla Rhino EngineVersion = 1.6 release 2 LanguageName = ECMAScript LanguageVersion = 1.6 Extensions = [js] Engine Alias = js Engine Alias = rhino Engine Alias = JavaScript Engine Alias = javascript Engine Alias = ECMAScript Engine Alias = ecmascript 1
for遍历了但始终只有一个结果,难道支持js的哇!