From the DynJS website:
- DynJS comes with a binary that you can use to execute Javascript files on the command line.
- DynJS can be embedded into an existing Java application to provide JavaScript scripting abilities from your Java projects.
Command Line
$ ./bin/dynjs --help
Usage: dynjs [--console |--debug | --help | --version |FILE]
Starts the dynjs console or executes FILENAME depending the parameters
FILE : File to be executed by dynjs
--console : Opens a REPL console to test small expressions.
--debug : Run REPL in debug mode.
--help : Shows current screen. Running without parameters also shows this.
--version : Shows current dynjs version.
$ cat my_app.js
print("Hi! What's your name?");
var sayHello = function(name) {
print("Hello " + name);
}
System = java.lang.System;
scanner = new java.util.Scanner(System.in);
name = scanner.nextLine();
sayHello(name);
$ ./bin/dynjs my_app.js
Hi! What's your name?
Douglas
Hello Douglas
^D
Embedding
import org.dynjs.Config;
import org.dynjs.exception.ThrowException;
import org.dynjs.runtime.*;
public class ScriptRunner {
DynJS dynjs;
Config config;
public ScriptRunner() {
config = new Config();
dynjs = new DynJS(config);
}
public Object runScript(String fileName) {
Runner runner = dynjs.newRunner();
return runner.withSource(new File(fileName)).execute();
}
public Object eval(String code) {
return dynjs.evaluate(code);
}
}