Last active
August 29, 2015 14:02
-
-
Save quidmonkey/6449bdbabd0f895dbc41 to your computer and use it in GitHub Desktop.
Rhino Example to Execute a Javascript File - Run the following command: "java JavaScriptExecuter test.js"
This file contains hidden or 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 java.io.FileReader; | |
| import java.io.Reader; | |
| import javax.script.Invocable; | |
| import javax.script.ScriptEngine; | |
| import javax.script.ScriptEngineManager; | |
| import javax.script.ScriptException; | |
| public class JavaScriptExecuter { | |
| public static void main(String[] args) throws FileNotFoundException, ScriptException, NoSuchMethodException { | |
| if (args.length == 0) { | |
| System.out.println("Missing File Name as an Argument"); | |
| System.exit(0); | |
| } | |
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); | |
| engine.put("engine", engine); | |
| engine.eval(new FileReader(args[0])); | |
| } | |
| } |
This file contains hidden or 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
| var foo = 1, | |
| bar = 2, | |
| baz = foo + bar, | |
| qux; | |
| function translate (a, b, c) { | |
| return a + b * c; | |
| } | |
| qux = translate(foo, bar, baz); | |
| print('And the answer is...' + qux + '!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment