java執行javascript

itscrambler發表於2012-11-23
String script = "function abc(x,y) {return x+y;}"
+ "function def(u,v) {return u-v;}";Context context = Context.enter();try {
ScriptableObject scope = context.initStandardObjects();
context
.evaluateString(scope, script, "script", 1, null);
Function fct = (Function)scope.get("abc", scope);
Object result = fct.call(
context
, scope, scope, new Object[] {2, 3});
System.out.println(Context.jsToJava(result, int.class));} finally {
Context.exit();}[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26273052/viewspace-1059811/,如需轉載,請註明出處,否則將追究法律責任。

相關文章