SoFunction
Updated on 2025-04-09

Introduction to the instructions for using different versions of jdk in java scripts

Instructions for using different versions of jdk in java scripts

In Java, there are several ways to run or execute JavaScript scripts, the most commonly used is to execute JavaScript code through Java's built-in JavaScript engine (Nashorn or Rhino).

The following are several implementation methods.

1. Execute JavaScript using ScriptEngine

Java introduced a package in JDK 6, which allows you to use scripting languages ​​in Java, including JavaScript. By JDK 8, the Nashorn JavaScript engine replaced the earlier Rhino engine (version below jdk6).

Sample code:

import ;
import ;
import ;

public class JavaScriptExample {
    public static void main(String[] args) {
        // Create ScriptEngineManager        ScriptEngineManager manager = new ScriptEngineManager();
        
        // Get the JavaScript engine        ScriptEngine engine = ("JavaScript");
        
        // JavaScript code to execute        String script = "var x = 10; var y = 20; x + y;";
        
        // Execute JavaScript code        try {
            Object result = (script);
            ("JavaScript Results: " + result);
        } catch (ScriptException e) {
            ();
        }
    }
}

Output:

JavaScript results: 30

(JDK 8 and above) and tool classes

In Java 8, the Nashorn engine is the default JavaScript engine. Nashorn's performance is better than the Rhino engine and provides better support for the ES5 standard.

JDK 8 uses Nashorn by default. If you are in JDK 11 or later, Nashorn may be removed, and other methods are recommended (such as GraalVM).

Based on most projects currently usedjdk8, given as followsDetailed tool classAvailable for use.

import ;
import ;
import ;
import ;

public class ScriptUtil {

    private final ScriptEngine engine;

    public ScriptUtil() {
        ScriptEngineManager manager = new ScriptEngineManager();
        // Get Nashorn engine instance        engine = ("nashorn");

    }

    // Execute any JavaScript code    public Object executeScript(String script) throws ScriptException {
        return (script);
    }

    // Execute JavaScript code with parameters    public Object executeScriptWithBindings(String script, Object... args) throws ScriptException {
        // Set parameters in the script        for (int i = 0; i < ; i++) {
            ("arg" + i, args[i]);
        }
        return (script);
    }

    // Call functions in JavaScript    public Object callFunction(String script, String functionName, Object... args) throws ScriptException, NoSuchMethodException {
        // Execute the script containing the function definition first        (script);
        // Get Invocable instance        Invocable invocable = (Invocable) engine;
        // Call the specified JavaScript function        return (functionName, args);
    }

    public static void main(String[] args) {
        try {
            //JavaScriptExecutor executor = new JavaScriptExecutor();
            ScriptUtil xbScriptUtil = new ScriptUtil();
            // Execute simple JavaScript code            String script = "print('Hello, World!');";
            Object object = (script);
            (object);
            // Execute JavaScript code with parameters            String scriptWithParams = "var result = arg0 + arg1; result;";
            Object result = (scriptWithParams, 5, 3);
            ("Result with bindings: " + result);

            // Call functions in JavaScript            String functionScript = "function add(a, b) { return a + b; }";
            Object functionResult = (functionScript, "add", 5, 3);
            ("Function result: " + ());

            String functionScript1 = "function calcData(val) { var val2='455'; return val+val2; }";
            //Passing in string            Object functionResult1 = (functionScript1, "calcData", "222");
            ("Function1 result: " + ());

            String functionScript2 = "function calcData(obj) { var val2=('11','22'); return val2; }";
            // Pass in object parameters and call function            Object functionResult2 = (functionScript2, "calcData", xbScriptUtil);
            ("Function2 result: " + ());
            // (("1","2"));

        } catch (ScriptException | NoSuchMethodException e) {
            ();
        }
    }

    public String getData(String channelId, String pointId) {
        return channelId + "#" + pointId;
    }
}

Output:

Hello, World!
null
Result with bindings: 8.0
Function result: 8.0
Function1 result: 222455
Function2 result: 11#22

(JDK 11 and above)

Starting with JDK 11, Oracle Modern allows removal of Nashorn, introducing a more complete solution that GraalVM multilingual support. GraalVM is an efficient execution of JavaScript, Python, R and other languages ​​in Java.

Execute JavaScript using GraalVM

You need to use GraalVM's polyglotAPI, here is an example of how to execute JavaScript code through GraalVM.

Sample code:

<dependency>
    <groupId></groupId>
    <artifactId>js</artifactId>
    <version>23.0.4</version> <!-- Please use the latest version -->
</dependency>
import ;
import ;

public class GraalVMExample {
    public static void main(String[] args) {
        // Create GraalVM context        try (Context context = ()) {
            // Execute JavaScript code            Value result = ("js", "var x = 10; var y = 20; x + y;");
            ("JavaScript Results: " + ());
        }
    }
}

Output:

JavaScript results: 30

Summarize

ScriptEngine (Nashorn/Rhino): Suitable for JDK 8 and below, suitable for simple JavaScript script execution.

GraalVM: It is recommended to use in JDK 11 and above, supports more languages, and has better performance.

If you need to integrate or execute a lot of complex JavaScript code in a Java project, it is recommended to use GraalVM. If you only need to run a small amount of simple JavaScript code, you can continue to use ScriptEngine.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.