日期:2014-05-16  浏览次数:20524 次

Mozilla Rhino:浣跨敤java璇█鏉ヨВ閲婂拰鎵цjavascript鑴氭湰(杞?

Mozilla Rhino:浣跨敤java璇█鏉ヨВ閲婂拰鎵цjavascript鑴氭湰(杞?聽聽

2009-05-25 09:37:32|聽聽鍒嗙被锛?/span> JS |瀛楀彿璁㈤槄

Mustang 鐨勮剼鏈紩鎿?/p>

JSR 233 涓?Java 璁捐浜嗕竴濂楄剼鏈瑷€ API銆傝繖涓€濂?API 鎻愪緵浜嗗湪 Java 绋嬪簭涓皟鐢ㄥ悇绉嶈剼鏈瑷€寮曟搸鐨勬帴鍙c€備换浣曞疄鐜颁簡杩欎竴鎺ュ彛鐨勮剼鏈瑷€寮曟搸閮藉彲浠ュ湪 Java 绋嬪簭涓璋冪敤銆傚湪 Mustang 鐨勫彂琛岀増鏈腑鍖呮嫭浜嗕竴涓熀浜?Mozilla Rhino 鐨?JavaScript 鑴氭湰寮曟搸銆?/p>

Mozilla Rhino

Rhino 鏄竴涓函 Java 鐨勫紑婧愮殑 JavaScript 瀹炵幇銆備粬鐨勫悕瀛楁潵婧愪簬 O'Reilly 鍏充簬 JavaScript 鐨勪功鐨勫皝闈細


Rhino 椤圭洰鍙互杩芥湐鍒?1997 骞达紝褰撴椂 Netscape 璁″垝寮€鍙戜竴涓函 Java 瀹炵幇鐨?Navigator锛屼负姝ら渶瑕佷竴涓?Java 瀹炵幇鐨?JavaScript 鈥斺€?Javagator銆傚畠涔熷氨鏄?Rhino 鐨勫墠韬€傝捣鍒?Rhino 灏?JavaScript 缂栬瘧鎴?Java 鐨勪簩杩涘埗浠g爜鎵ц锛岃繖鏍峰畠浼氭湁鏈€濂界殑鎬ц兘銆傚悗鏉ョ敱浜庣紪璇戞墽琛岀殑鏂瑰紡瀛樺湪鍨冨溇鏀堕泦鐨勯棶棰樺苟涓旂紪璇戝拰瑁呰浇杩囩▼鐨勫紑閿€杩囧ぇ锛屼笉鑳芥弧瓒充竴浜涢」鐩殑闇€姹傦紝Rhino 鎻愪緵浜嗚В閲婃墽琛岀殑鏂瑰紡銆傞殢鐫€ Rhino 寮€鏀炬簮浠g爜锛岃秺鏉ヨ秺澶氱殑鐢ㄦ埛鍦ㄨ嚜宸辩殑浜у搧涓娇鐢ㄤ簡 Rhino锛屽悓鏃朵篃鏈夎秺鏉ヨ秺澶氱殑寮€鍙戣€呭弬涓庝簡 Rhino 鐨勫紑鍙戝苟鍋氬嚭浜嗗緢澶х殑璐$尞銆傚浠?Rhino1.6R2 鐗堟湰灏嗚鍖呭惈鍦?Java SE6 涓彂琛岋紝鏇村鐨?Java 寮€鍙戣€呭皢浠庝腑鑾风泭銆?/p>

Rhino 鎻愪緵浜嗗涓嬪姛鑳?/p>

瀵?JavaScript 1.5 鐨勫畬鍏ㄦ敮鎸?br>鐩存帴鍦?Java 涓娇鐢?JavaScript 鐨勫姛鑳?br>涓€涓?JavaScript shell 鐢ㄤ簬杩愯 JavaScript 鑴氭湰
涓€涓?JavaScript 鐨勭紪璇戝櫒锛岀敤浜庡皢 JavaScript 缂栬瘧鎴?Java 浜岃繘鍒舵枃浠?br>鏀寔鐨勮剼鏈瑷€

鍦?dev.java.net鍙互鎵惧埌瀹樻柟鐨勮剼鏈紩鎿庣殑瀹炵幇椤圭洰銆傝繖涓€椤圭洰鍩轰簬BSD License 锛岃〃绀鸿繖浜涜剼鏈紩鎿庣殑浣跨敤灏嗗崄鍒嗚嚜鐢便€傜洰鍓嶈椤圭洰宸插鍖呮嫭 Groovy, JavaScript, Python, Ruby, PHP 鍦ㄥ唴鐨勪簩鍗佸绉嶈剼鏈瑷€鎻愪緵浜嗘敮鎸併€傝繖涓€鏀寔鍒楄〃杩樺皢涓嶆柇鎵╁ぇ銆?/p>

鍦?Mustang 涓鑴氭湰寮曟搸鐨勬绱娇鐢ㄤ簡宸ュ巶妯″紡銆傞鍏堥渶瑕佸疄渚嬪寲涓€涓伐鍘?鈥斺€?ScriptEngineManager銆?/p>

// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();聽


ScriptEngineManager 灏嗗湪 Thread Context ClassLoader 鐨?Classpath 涓牴鎹?jar 鏂囦欢鐨?META-INF 鏉ユ煡鎵惧彲鐢ㄧ殑鑴氭湰寮曟搸銆傚畠鎻愪緵浜?3 绉嶆柟娉曟潵妫€绱㈣剼鏈紩鎿庯細

// create engine by name
ScriptEngine engine = factory.getEngineByName ("JavaScript");
// create engine by name
ScriptEngine engine = factory.getEngineByExtension ("js");
// create engine by name
ScriptEngine engine = factory.getEngineByMimeType ("application/javascript");聽


涓嬮潰鐨勪唬鐮佸皢浼氭墦鍗板嚭褰撳墠鐨?JDK 鎵€鏀寔鐨勬墍鏈夎剼鏈紩鎿?/p>

ScriptEngineManager factory = new ScriptEngineManager();
for (ScriptEngineFactory available : factory.getEngineFactories()) {
System.out.println(available.getEngineName());
}聽


浠ヤ笅鍚勭珷鑺備唬鐮佸皢浠?JavaScript 涓轰緥銆?/p>

鍦?Java 涓В閲婅剼鏈?/p>

鏈変簡鑴氭湰寮曟搸瀹炰緥灏卞彲浠ュ緢鏂逛究鐨勬墽琛岃剼鏈瑷€锛屾寜鐓ф儻渚嬶紝鎴戜滑杩樻槸浠庝竴涓畝鍗曠殑 Hello World 寮€濮嬶細

public class RunJavaScript {
public static void main(String[] args){
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName ("JavaScript");
engine.eval("print('Hello World')");
}
}聽


杩欐 Java 浠g爜灏嗕細鎵ц JavaScript 骞舵墦鍗板嚭 Hello World銆傚鏋?JavaScript 鏈夎娉曢敊璇皢浼氬浣曪紵

engine.eval("if(true){println ('hello')");聽

鏁呮剰娌℃湁鍔犱笂鈥潁鈥濓紝鎵ц杩欐浠g爜 Java 灏嗕細鎶涘嚭涓€涓?javax.script.ScriptException 骞跺噯纭殑鎵撳嵃鍑洪敊淇℃伅锛?Exception in thread "main" javax.script.ScriptException:
sun.org.mozilla.javascript.internal.EvaluatorException:
missing } in compound statement (<Unknown source>#1) in <Unknown source>
at line number 1
at ...聽


濡傛灉鎴戜滑瑕佽В閲婁竴浜涙洿澶嶆潅鐨勮剼鏈瑷€锛屾垨鑰呮兂鍦ㄨ繍琛屾椂鏀瑰彉璇ヨ剼鏈濡備綍鍋氬憿锛熻剼鏈紩鎿庢敮鎸佷竴涓噸杞界殑 eval 鏂规硶锛屽畠鍙互浠庝竴涓?Reader 璇诲叆鎵€闇€鐨勮剼鏈細

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName ("JavaScript");
engine.eval(new Reader("HelloWorld.js"));聽