Skip to content

Instantly share code, notes, and snippets.

@eleco
Created July 25, 2014 16:37
Show Gist options
  • Save eleco/644b63f2ec9000f3fa64 to your computer and use it in GitHub Desktop.
Save eleco/644b63f2ec9000f3fa64 to your computer and use it in GitHub Desktop.
evaluate javascript function with mozilla's rhino
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;
public class RhinoLoader {
public static void main(String args[]){
Context ctx = Context.enter();
try {
Scriptable scope = ctx.initStandardObjects();
TestJs testJs = new TestJs();
scope.put("steps",scope,testJs);
NativeJavaObject result =(NativeJavaObject) (ctx.evaluateString(scope, "steps.myfunction()","test",1,null)) ;
System.out.println("result :" +result.unwrap());
} finally{
Context.exit();
}
}
}
public class TestJs {
public String myfunction(){
return "1";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment