計算式をデータベースにマスタ登録するような場合に使うのね。
(いま、そういう必要性が出ただけ。。)
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test {
public static void main(String[] args) throws Exception {
String script = "result=num*2";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Compilable ce = (Compilable) engine;
CompiledScript cs = ce.compile(script);
engine.put("num", 1);
cs.eval();
Object result = engine.get("result");
if(result != null) {
System.out.println("result=" + result.toString());
}
}
}