Passing float value Java Parameters into Javascript - Java Scripting

Java examples for Scripting:Bindings

Description

Passing float value Java Parameters into Javascript

Demo Code

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;

public class Main {
  public static void main(String[] args) throws Exception {

    float width = 6;
    float length = 2;
    float depth = 5;

    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine nashorn = sem.getEngineByName("nashorn");

    SimpleBindings simpleBindings2 = new SimpleBindings();
    simpleBindings2.put("globalWidth", width);
    simpleBindings2.put("globalLength", length);
    simpleBindings2.put("globalDepth", depth);
    nashorn//from w  ww.java  2s  .  c  o  m
        .eval(
            "function a(width, length, avgDepth){var volume = avgDepth * width * length; "
                + "        return volume * 8.8; }   "
                + "print(a(globalWidth, globalLength, globalDepth));",
            simpleBindings2);

  }

}

Result


Related Tutorials