Passing Parameters From a Java Program to Scripts - Java Scripting

Java examples for Scripting:Run Script

Description

Passing Parameters From a Java Program to Scripts

Demo Code

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

public class Main {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "print(msg)";

    try {/*from  ww  w  .j  av  a  2s.  c o  m*/
      engine.put("msg", "Hello from Java program");
      engine.eval(script);
    }
    catch (ScriptException e) {
      e.printStackTrace();
    }
  }
}

Result


Related Tutorials