Scripting in Java Tutorial - Scripting in Java eval








A ScriptEngine can execute a script in a String and a java.io.Reader.

By using Reader, we can execute a script on the network or in a file.

eval() method from the ScriptEngine interface has the following overloaded versions.

Object eval(String script)
Object eval(Reader reader)
Object eval(String script, Bindings bindings)
Object eval(Reader reader, Bindings bindings)
Object eval(String script, ScriptContext context)
Object eval(Reader reader, ScriptContext context)




Example

The following code shows how to run Javascript script code stored from a .js file.

The contents of .js file named helloscript.js is listed as follows.

// Print a message
print('Hello from JavaScript!');

Here is the code to run the script.

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/* w  w  w.ja  v a2 s .c o  m*/
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {
  public static void main(String[] args) {
    String scriptFileName = "c:/test.js";
    Path scriptPath = Paths.get(scriptFileName);
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    try {
      Reader scriptReader = Files.newBufferedReader(scriptPath);
      engine.eval(scriptReader);
    } catch (IOException | ScriptException e) {
      e.printStackTrace();
    }
  }
}

The code above generates the following result.





Example 2

The eval() method from ScriptEngine returns the last value in the script as an Object.

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

    Object result = null;
    result = engine.eval("1 + 2;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4; var v = 5; v = 6;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4; var v = 5;");
    System.out.println(result);
    result = engine.eval("print(1 + 2)");
    System.out.println(result);

  }
}

The code above generates the following result.

Example 4

The following code shows how to pass a Result object to a script that populates the Result object with a value.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/*  w  w w  .j a v  a2s.c om*/
public class Main {
  private int val = -1;

  public void setValue(int x) {
    val = x;
  }

  public int getValue() {
    return val;
  }
  public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    Main result = new Main();
    engine.put("result", result);
    String script = "3 + 4; result.setValue(1);";
    engine.eval(script);
    int returnedValue = result.getValue();
    System.out.println("Returned value is " + returnedValue);
  }
}

The code above generates the following result.