For example, I have version 21 installed and as far as I understand, I can’t use Nashorn engine anymore.
But maybe GraalVM is available for me?
I have installed all the necessary dependencies:
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>21.1.0</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>21.1.0</version>
</dependency>
And here’s a simple example that I’m trying to execute:
public static void main(String[] args) throws ScriptException {
try (Context context = Context.newBuilder("js")
.allowAllAccess(true)
.build()) {
// Evaluate JavaScript code
String jsCode = "console.log('Hello, GraalVM!');";
context.eval("js", jsCode);
} catch (Exception e) {
throw new ScriptException("Script execution failed: " + e.getMessage());
}
}
However, I get an error:
Exception in thread “main” javax.script.ScriptException: Script
execution failed: A language with id ‘js’ is not installed. Installed
languages are: []. at org.example.Main.main(Main.java:23)
I also tried something like this:
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("print('HI');");
}
But I got another error:
Exception in thread “main” java.lang.NullPointerException: Cannot
invoke “javax.script.ScriptEngine.eval(String)” because “engine” is
null at org.example.Main.main(Main.java:20)
The problem is that manual installation of any components is impossible for some reason. I just need to some dependencies and make everything works. Something “out of the box”. Is there any workaround for this problem? Maybe there are any other available engines?
Thanks everyone.