Passing data between Java and JavaScript in Nashorn

Nashorn is a JavaScript engine that comes bundled with Java 8 and later versions. It provides a way to execute JavaScript code within Java applications. One of the key features of Nashorn is the ability to pass data back and forth between Java and JavaScript seamlessly. In this blog post, we’ll explore different ways to pass data between Java and JavaScript in Nashorn.

Table of Contents

  1. Using Bindings
  2. Invoking Java Methods from JavaScript
  3. Passing Data to JavaScript
  4. Conclusion

Using Bindings

One way to pass data between Java and JavaScript in Nashorn is by using the Bindings class. Bindings acts as a bridge between the two languages, allowing you to share variables and objects.

To pass data from Java to JavaScript, you can create a Bindings object, put your data into it, and then set this object as the engine’s context:

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

Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("myVar", "Hello from Java");

engine.eval("print(myVar)");

In the above example, we create a Bindings object, put the string value "Hello from Java" into it, and then set it as the engine’s context. We can then access the myVar variable in the JavaScript code and print its value.

Invoking Java Methods from JavaScript

Nashorn allows you to invoke Java methods directly from JavaScript code. You can pass data to these methods from your JavaScript code and receive the result back.

To invoke a Java method from JavaScript, you first need to expose the Java class and its methods to the JavaScript code using the engine.put() method. Then, you can call the Java method from JavaScript using the assigned variable:

public class MyJavaClass {
    public static int add(int a, int b) {
        return a + b;
    }
}

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

engine.put("myJavaClass", MyJavaClass.class);

int result = (int) engine.eval("myJavaClass.add(5, 7)");
System.out.println(result); // Output: 12

In the above example, we define a simple Java class with a static add() method that adds two integers. We then expose this class to the JavaScript code using engine.put() and call the add() method from JavaScript. Lastly, we print the result obtained from the Java method call.

Passing Data to JavaScript

Similarly, you can pass data from Java to JavaScript by exposing Java objects or variables to the JavaScript code. Nashorn provides several methods to do this, including engine.put() and engine.eval().

Here’s an example that demonstrates passing a Java object to JavaScript:

public class MyJavaClass {
    public String getData() {
        return "Hello from Java";
    }
}

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

MyJavaClass myJavaObject = new MyJavaClass();
engine.put("myObject", myJavaObject);

engine.eval("print(myObject.getData())");

In this example, we create an instance of MyJavaClass and expose it to the JavaScript code using engine.put(). We can then access the methods and properties of the Java object in JavaScript and print its data.

Conclusion

Passing data between Java and JavaScript in Nashorn is made easy through the use of Bindings, invoking Java methods from JavaScript, and exposing Java objects and variables to the JavaScript code. This functionality allows you to seamlessly integrate both languages in your Java applications and leverage the power and flexibility of JavaScript within a Java environment. #java #javascript