Created
July 25, 2014 16:37
-
-
Save eleco/644b63f2ec9000f3fa64 to your computer and use it in GitHub Desktop.
evaluate javascript function with mozilla's rhino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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