Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save quidmonkey/6449bdbabd0f895dbc41 to your computer and use it in GitHub Desktop.

Select an option

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"
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]));
}
}
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