On Wednesday, August 26, 2015 at 1:17:06 PM UTC-4, Stefan Karpinski wrote:
>
> Is JavaScript actually easy to integrate with Java?
>
Java has always come bundled with a Javascript interpreter (first Rhino,
now Nashorn). It can be used like this:
*package sample1;*
*import javax.script.ScriptEngine;*
*import javax.script.ScriptEngineManager;*
*public class* Hello {
* public* static void main(String... args) *throws* Throwable {
ScriptEngineManager engineManager =
*new* ScriptEngineManager();
ScriptEngine engine =
engineManager.getEngineByName("nashorn");
engine.eval("function sum(a, b) { return a + b; }");
System.out.println(engine.eval("sum(1, 2);"));
}
}
I took this example from the docs:
http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html