Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Nashorn JavaScript Engine in Java 8

Overview

Java 8 introduced the Nashorn JavaScript engine, which allows developers to execute JavaScript code within the Java Virtual Machine (JVM). Nashorn provides better performance and compliance with the ECMAScript specification compared to its predecessor, Rhino.

Executing JavaScript Code

You can execute JavaScript code using the Nashorn engine by leveraging the javax.script package.

Example: Executing JavaScript Code

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

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

        String script = "print('Hello, World!')";
        engine.eval(script);
    }
}

Passing Variables between Java and JavaScript

You can pass variables between Java and JavaScript using the put and get methods of the ScriptEngine interface.

Example: Passing Variables

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

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

        engine.put("message", "Hello from Java!");
        engine.eval("var response = message + ' and Nashorn';");
        String response = (String) engine.get("response");

        System.out.println(response);
    }
}

Invoking JavaScript Functions from Java

You can define JavaScript functions and invoke them from Java using the Invocable interface.

Example: Invoking JavaScript Functions

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

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

        String script = "function greet(name) { return 'Hello, ' + name; }";
        engine.eval(script);

        Invocable invocable = (Invocable) engine;
        String result = (String) invocable.invokeFunction("greet", "World");

        System.out.println(result);
    }
}

Accessing Java Classes from JavaScript

Nashorn allows JavaScript code to create and use instances of Java classes, providing seamless integration between Java and JavaScript.

Example: Accessing Java Classes

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

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

        String script = "var ArrayList = Java.type('java.util.ArrayList');" +
                        "var list = new ArrayList();" +
                        "list.add('Apple');" +
                        "list.add('Banana');" +
                        "list.add('Cherry');" +
                        "list";

        Object result = engine.eval(script);
        System.out.println(result);
    }
}

Running JavaScript Files

You can execute JavaScript code from external files using the Nashorn engine.

Example: Running JavaScript Files

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileReader;
import java.io.IOException;

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

        engine.eval(new FileReader("path/to/your/script.js"));
    }
}

Conclusion

The Nashorn JavaScript engine in Java 8 provides a powerful and flexible way to execute JavaScript code within the JVM. By leveraging Nashorn, developers can integrate JavaScript and Java seamlessly, allowing for a wide range of use cases from simple scripting to complex application logic.