This example shows how to get an instance of ScriptEngine
by the engine name. Below we are trying to obtain the GraalVM JavaScript engine.
package org.kodejava.script;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public class GettingScriptEngineByName {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
// Obtain an instance of ScriptEngine using the engine name. For
// example, we get a GraalVM JavaScript engine.
ScriptEngine engine = manager.getEngineByName("graal.js");
try {
engine.eval("print('Hello World');");
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>22.3.2</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>22.3.2</version>
</dependency>
</dependencies>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024