Java - ScriptEngineManager example

Evaluating a Statement

In this example, the eval() method is called on the script engine instance to execute
JavaScript code from a String object.

import javax.script.*;
public class EvalScript {
 public static void main(String[] args) throws Exception {
 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByName("nashorn");
 // evaluate JavaScript code
 engine.eval("print('Hello, World')");
 }
}

Evaluating a Script File

In this example, the eval() method takes in a FileReader object that reads JavaScript
code from a file named script.js. By wrapping various input stream objects as
readers, it is possible to execute scripts from files, URLs, and other resources.

import javax.script.*;
public class EvalFile {
 public static void main(String[] args) throws Exception {
 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByName("nashorn");
 // evaluate JavaScript code
 engine.eval(new java.io.FileReader("script.js"));
 }
}

Exposing a Java Object as a Global Variable

In this example, a File object is created and exposed to the engine as a global
variable named file using the put() method. Then the eval() method is called with
JavaScript code that accesses the variable and calls the getAbsolutePath() method.

import javax.script.*;
import java.io.*;
public class ScriptVars {
 public static void main(String[] args) throws Exception {
 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByName("nashorn");
 // create File object
 File f = new File("test.txt");
 // expose File object as a global variable to the engine
 engine.put("file", f);
 // evaluate JavaScript code and access the variable
 engine.eval("print(file.getAbsolutePath())");
 }
}

Invoking a Script Function

In this example, the eval() method is called with JavaScript code that defines a
function with one parameter. Then, an Invocable object is created and its
invokeFunction() method is used to invoke the function.

import javax.script.*;
public class InvokeScriptFunction {
 public static void main(String[] args) throws Exception {
 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByName("nashorn");
 // evaluate JavaScript code that defines a function with one parameter
 engine.eval("function hello(name) { print('Hello, ' + name) }");
// create an Invocable object by casting the script engine object
 Invocable inv = (Invocable) engine;
 // invoke the function named "hello" with "Scripting!" as the argument
 inv.invokeFunction("hello", "Scripting!");
 }
}


Invoking a Script Object's Method 


In this example, the eval() method is called with JavaScript code that defines an object with a method. This object is then exposed from the script to the Java application using the script engine's get() method. Then, an Invocable object is created, and its invokeMethod() method is used to invoke the method defined for the script object.

 import javax.script.*;
public class InvokeScriptMethod {
 public static void main(String[] args) throws Exception {
 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByName("nashorn");
 // evaluate JavaScript code that defines an object with one method
 engine.eval("var obj = new Object()");
 engine.eval("obj.hello = function(name) { print('Hello, ' + name) }");
 // expose object defined in the script to the Java application
 Object obj = engine.get("obj");
 // create an Invocable object by casting the script engine object
 Invocable inv = (Invocable) engine;
 // invoke the method named "hello" on the object defined in the script
 // with "Script Method!" as the argument
 inv.invokeMethod(obj, "hello", "Script Method!");
 }
}



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation