Function Evaluator : ScriptEngines « Development « Java Tutorial






import java.io.FileReader;
import java.io.IOException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class FuncEvaluator {
  public static void main(String[] args) {
    if (args.length != 2) {
      System.err
          .println("usage: java FuncEvaluator scriptfile " + "script-exp");
      return;
    }

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("rhino");

    try {
      System.out.println(engine.eval(new FileReader(args[0])));
      System.out.println(engine.eval(args[1]));
    } catch (ScriptException se) {
      System.err.println(se.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
  }
}
//////////////
function combinations (n, r)
{
   return fact (n)/(fact (r)*fact (n-r))
}

function fact (n)
{
   if (n == 0)
       return 1;
   else
       return n*fact (n-1);
}








6.48.ScriptEngines
6.48.1.Obtain ScriptEngine
6.48.2.Enumerate ScriptEngines
6.48.3.Function Evaluator
6.48.4.Bindings And Scopes
6.48.5.Pass value and get return value from script
6.48.6.Temperature Conversion with script
6.48.7.Test Compilation Speed
6.48.8.Threading Behavior