public static void Main (string[] args) throws FileNotFoundException, Scriptexception, nosuchmethodexception{
ScriptEngine engine = new Scriptenginemanager (). Getenginebyname ("JavaScript");
Setting up a contextual environment
Bindings bind = Engine.createbindings ();
Bind.put ("factor", 1);
Binding context, scope is current engine scope
Engine.setbindings (Bind, Scriptcontext.engine_scope);
Receive parameter input
Scanner Scanner = new Scanner (system.in);
while (Scanner.hasnextint ()) {
int first = Scanner.nextint ();
int second = Scanner.nextint ();
SYSTEM.OUT.PRINTLN ("Receive parameter:" +first+ "," +second ");
Engine Execution Script
Engine.eval (New FileReader ("C:/modal.js"));
Determines whether an executable
if (engine instanceof invocable) {
Invocable in = (invocable) engine;
Double result = (double) in.invokefunction ("formula", First,second);
System.out.println ("Execution result:" +result);
}
}
}
The contents of the Modal.js script under C disk are as follows:
function formula (value1,value2) {
return value1+value2-factor;
}
Java program calls JavaScript and other scripts to implement the method