Java examples for Scripting:Run Script
Get return value from Loading and Executing internal JavaScript from Java
import java.util.logging.Level; import java.util.logging.Logger; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Main { public static void main(String[] args) { ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine nashorn = sem.getEngineByName("nashorn"); try {// w w w . j av a 2 s . c o m nashorn.eval("function a(width, length, avgDepth){var volume = avgDepth * width * length;" + " return volume * 0.8; }"); Double a = (Double) nashorn.eval("a(6,3,5);"); System.out.println(a); } catch (ScriptException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }