Scripting in Java Tutorial - Scripting in Java Parameters








The Java Scripting API allows us to pass parameters from Java application to the script engine and vice versa.

Passing Parameters from Java Code to Scripts

msg is a variable used in the print() function.

The script does not declare the msg variable.

We can pass a parameter to a script engine in several ways.

One way is to use the put(String paramName, Object paramValue) method of the script engine, which accepts two arguments:

  • the name of the parameter, which needs to match the name of the variable in the script.
  • the value of the parameter.

To pass a parameter named msg to the script engine, call the put() method as follows:

engine.put("msg", "Hello from Java program");

The following code shows how to pass Parameters From a Java Program to Scripts.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
//w  ww  . j av a2s . c  o  m
public class Main {
  public static void main(String[] args) {
    // Get the Nashorn engine
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    String script = "print(msg)";
    try {
      engine.put("msg", "Hello from Java program");
      engine.eval(script);
    } catch (ScriptException e) {
      e.printStackTrace();
    }
  }
}

The code above generates the following result.

To pass a parameter named msg to a script in JRuby.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");

String script = "puts($msg)";
engine.put("msg", "Hello from Java");

// Execute the script
engine.eval(script);





Example

Properties and methods of Java objects passed to scripts can be accessed in scripts

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/*from w ww . j a  va  2 s .c  o m*/
public class Main {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    String script = "print(msg.toUpperCase());print(msg.indexOf('a'))";
    engine.put("msg", "java2s.com");
    engine.eval(script);
  }
}

The code above generates the following result.





Passing Parameters from Scripts to Java Code

A script engine can pass global scope variables to Java code.

The get(String variableName) method from ScriptEngine can access those variables in Java code.

It returns a Java Object.

The declaration of a global variable is scripting-language-dependent.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/*  ww  w .  j av  a  2s  .co m*/
public class Main {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    String script = "var year = 2015";

    engine.eval(script);
    Object year = engine.get("year");
    System.out.println("year's class:" + year.getClass().getName());
    System.out.println("year's value:" + year);
  }
}

The code above generates the following result.