Java Language – 78 – Nashorn JavaScript Engine

Java 8 Features – Nashorn JavaScript Engine
Introduction to Nashorn JavaScript Engine

Java 8 introduced the Nashorn JavaScript Engine, which is a powerful feature that enables developers to seamlessly integrate JavaScript code with Java applications. It provides a faster and more efficient way to execute JavaScript code within a Java environment.

Executing JavaScript Code

To execute JavaScript code using Nashorn, you can create a scripting engine and then evaluate JavaScript expressions. Here’s a simple example of how to execute JavaScript code within a Java application:


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

public class NashornExample {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("nashorn");

        String javascriptCode = "print('Hello, Nashorn!');";
        engine.eval(javascriptCode);
    }
}
Integration with Java Code

One of the main benefits of Nashorn is its ability to seamlessly integrate JavaScript code with Java code. You can call Java methods from JavaScript and vice versa. This allows developers to leverage the strengths of both languages in a single application.

Here’s an example of calling a Java method from JavaScript:


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

public class NashornIntegrationExample {
    public static void main(String[] args) throws ScriptException, NoSuchMethodException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("nashorn");

        engine.eval("function sayHello(name) { print('Hello, ' + name); }");

        Invocable invocable = (Invocable) engine;
        invocable.invokeFunction("sayHello", "Nashorn");
    }
}
Accessing Java Objects

Nashorn also allows you to access and manipulate Java objects from JavaScript. You can pass Java objects to JavaScript, interact with their methods and fields, and even create new Java objects.

Here’s an example of accessing a Java object from JavaScript:


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

public class NashornAccessingJavaExample {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("nashorn");

        // Pass a Java object to JavaScript
        engine.put("person", new Person("John", 30));

        String javascriptCode = "print('Name: ' + person.name + ', Age: ' + person.age);";
        engine.eval(javascriptCode);
    }
}

class Person {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
Performance Benefits

The Nashorn JavaScript Engine is designed to provide improved performance compared to the earlier Rhino engine. It uses just-in-time (JIT) compilation, which can significantly boost the execution speed of JavaScript code in Java applications.

Developers can benefit from these performance improvements when integrating JavaScript in their Java applications, making it a valuable feature in Java 8.

Conclusion

The Nashorn JavaScript Engine in Java 8 is a powerful tool for developers looking to integrate JavaScript seamlessly into their Java applications. It allows for the execution of JavaScript code, integration with Java code, and efficient access to Java objects. With the performance benefits it offers, Nashorn is a valuable feature for developers building applications that require the flexibility of both Java and JavaScript.